根据系统状态处理控件事件

时间:2015-04-25 20:08:23

标签: c# winforms simulation simulator

我试图构建Ingenico POS终端(iWL220)的模拟器。 主屏幕我有一个组合框。一旦用户输入id和密码,组合框就会加载6个菜单。如果用户单击btn1,则组合框清除菜单并添加另一组菜单。如果用户单击btn1以获取新加载的菜单,则再次清除组合框并加载另一组菜单。

我的问题是每次点击按钮(btn1,btn2,btn3,btn4,btn5)我必须编写很多if else语句。实施例;

第一个菜单(在组合框中)有6个扇区。

  • 1.SectorA
  • 2.SectorB
  • 3.SectorC
  • 4.SectorD
  • 5.SectorE
  • 6.SectorF

如果用户选择1.SectorA,则用户单击btn1。然后btn1清除组合框并加载另一组菜单。这个时间菜单(在组合框中)有3家公司。

  • 1.CompanyA
  • 2.CompanyB
  • 3.CompanyC

这次用户选择1.CompanyA然后用户再次点击btn1。然后btn1清除组合框并加载另一组菜单。此时间菜单(在组合框中)有2个付款选项。

  • 1.FullPayment
  • 2.ParitalPayment

现在,如果用户单击btn1或btn2,则可见的组合框变为false,并且在主屏幕中有一个标签和文本框。文本框允许用户输入用户号码并按Enter键(绿色按钮)。

我已经将Ingenico终端图片加载为jpeg和顶部我设置了我的按钮。 我只提供了我的模拟的小版本。在我的应用程序中,用户可以选择114个概率。

在我的应用程序中,btn1有92个被点击的概率,btn2有53个被点击的概率,依此类推。用户输入用户号码并单击绿色按钮后,我的应用程序使用wfc服务格式化数据并发送到sql server。 但是在用户点击按钮的每个组合之前,在我的应用程序中我将btn编号存储为422.这422意味着,用户选择了SectorD + CompanyB + ParitalPayment选项。所以我的wfc会知道它是什么意思422。

我的问题是为这个概率案例构建我的按钮事件的最短路径是什么?

我有4个按钮。 Btn1,Btn2,Btn3和Btn4。我还有一些如下所示的数组和1个组合框。

1.ArrayMain() = {“1.Water”,”2.Air”,”3.Soil”,”4.Fire”}
   1.1. ArrayWater() = {“1.Salty”,”2.Fresh”, “3.Contaminated”}
      1.1.1.ArraySalty() = {1.”AA”, 2.”BB”, 3.”CC”}
      1.1.2.ArrayFresh() = {1.”DD”, 2.”EE”, 3.”FF”}
      1.1.3.ArrayContaminated() = {1.”XX”, 2.”YY”, 3.”ZZ”}                              

1.2   ArrayAir() = {“1.Fresh”, “2.Contaminated”}
1.3   ArraySoil() = {“1.Normal”, “2.Contaminated”}
1.4   ArrayFire() = {“1.Low”,”2.Mid”,”3.High”}

当我的应用程序启动时,第一个数组值1.(ArrayMain)将填充comboBox。这个组合框将有4个值,其中包括“1.Water”,“2.Air”,“3.Soil”,“4.Fire”。如果用户选择“1.Water”而不是用户点击Btn1。比btn1事件清除comboBox并将1.1ArrayWater()值加载到comboBox。

第二次如果用户选择“1.Salty”而不是用户再次点击btn1,这次btn1事件清除comboBox并将1.1.1ArraySalty()值加载到comboBox中。

如果用户选择“2.BB”而不是用户点击Btn2并发送信息“BB”进行计算,则第三次。

Change ComboBox Values with button selection

首先你有5个(或多或少)菜单项,每次按下任何(数字)按钮(在pos终端中1到9 lilke),屏幕上会出现新菜单。

1 个答案:

答案 0 :(得分:3)

任何特定时间的每个按钮都应根据系统状态执行一些特定操作。显然,如果您尝试根据众多不同变量决定具体操作,您将创建大量分支代码。这样的代码很难正确编写,甚至更难以调试和维护。

那么,如果我们在某个特定的类(接口)中封装每个可能状态(状态序列)的当前操作,该怎么办:

/// <summary>
/// Represents internal terminal presenter that is used inside IGlobalTerminalPresenter.
/// </summary>
public interface ITerminalPresenter
{
    void UpdateUI();
    ITerminalPresenter this[Int32 index]
    {
        get;
    }
    ITerminalPresenter Do1();
    ITerminalPresenter Do2();
    ITerminalPresenter Parent
    {
        get;
        set;
    }
    void Reset();
}

在表单中,我们将使用类似接口的字段来封装演示者的所有更改。

/// <summary>
/// Represents terminal presenter that UI can operate upon.
/// </summary>
public interface IGlobalTerminalPresenter
{
    void UpdateUI();

    void Do1();

    void Do2();

    Int32 SelectedIndex
    {
        get;
        set;
    }

    void Reset();
}

我们的事件处理程序将成为:

    private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        var senderComboBox = (ComboBox)sender;

        this.globalTerminalPresenter.SelectedIndex = senderComboBox.SelectedIndex;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.globalTerminalPresenter.Do1();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.globalTerminalPresenter.Do2();
    }

为了让我们的具体TerminalPresenters与表单进行互操作,我们将强制我们的表单实现以下接口:

/// <summary>
/// This represents your UI in technology-independent manner
/// </summary>
public interface ITerminalView
{
    String Title { get; set; }
    String Input { get; set; }
    String Output { get; set; }
    String Button1_Text { get; set; }
    String Button2_Text { get; set; }
    IEnumerable<String> SelectionItems { get; set; }
    void Clear();
}

public partial class MainForm : Form,
    ITerminalView
{
    ...
    #region ITerminalView implementation

    public string Title
    {
        get { return this.Text; }
        set { this.Text = value; }
    }

    public String Button1_Text
    {
        get { return this.button1.Text; }
        set { this.button1.Text = value; }
    }

    public String Button2_Text
    {
        get { return this.button2.Text; }
        set { this.button2.Text = value; }
    }

    public string Input
    {
        get { return this.textBox_Input.Text; }
        set { this.textBox_Input.Text = value; }
    }

    public string Output
    {
        get { return this.textBox_Output.Text; }
        set { this.textBox_Output.Text = value; }
    }

    public IEnumerable<string> SelectionItems
    {
        get { return this.comboBox.Items.Cast<String>(); }
        set
        { 
            this.comboBox.Items.Clear();

            if (value == null)
                return;

            foreach (var item in value)
            {
                this.comboBox.Items.Add(item);
            }
        }
    }

    public void Clear()
    {
        this.comboBox.SelectedIndex = -1;
        this.Title = String.Empty;
        this.Input = String.Empty;
        this.Output = String.Empty;
        this.SelectionItems = null;
    }

    #endregion

现在我们将创建两个TerminalPresenters - 一个只允许通过组合框选择下一个选项,一个计算两个数字的总和。它们都使用相同的基类。

/// <summary>
/// Base class for all presenters
/// </summary>
public abstract class TerminalPresenterBase : ITerminalPresenter
{
    protected ITerminalView view;

    public TerminalPresenterBase(ITerminalView view)
    {
        if (view == null) 
            throw new ArgumentNullException("view");

        this.view = view;
        this.Parent = this;
    }

    public abstract void UpdateUI();

    public abstract ITerminalPresenter this[int index]
    {
        get;
    }

    public abstract ITerminalPresenter Do1();
    public abstract ITerminalPresenter Do2();

    public virtual ITerminalPresenter Parent
    {
        get;
        set;
    }

    public virtual void Reset()
    {
        this.UpdateUI();
    }
}

/// <summary>
/// Presenter whose sole goal is to allow user to select some other option and press next
/// </summary>
public class SelectOptionPresenter : TerminalPresenterBase
{
    private IList<KeyValuePair<String, ITerminalPresenter>> options;
    private ITerminalPresenter selected;
    private String title;

    public SelectOptionPresenter(ITerminalView view,
        String title, 
        IList<KeyValuePair<String, ITerminalPresenter>> options)
        : base(view)
    {
        if (options == null)
            throw new ArgumentNullException("options");

        this.title = title;

        this.options = options;

        foreach (var item in options)
        {
            item.Value.Parent = this;
        }
    }

    public override void UpdateUI()
    {
        this.view.Clear();

        this.view.Button1_Text = "Confirm selection";
        this.view.Button2_Text = "Go back";
        this.view.Title = title;
        this.view.SelectionItems = options
            .Select(opt => opt.Key);
    }

    public override ITerminalPresenter this[int index]
    {
        get
        {
            this.selected = this.options[index].Value;

            return this;
        }
    }

    public override ITerminalPresenter Do1()
    {
        return this.ConfirmSelection();
    }

    public override ITerminalPresenter Do2()
    {
        return this.GoBack();
    }

    public ITerminalPresenter ConfirmSelection()
    {
        this.selected.UpdateUI();
        return this.selected;
    }

    public ITerminalPresenter GoBack()
    {
        this.Parent.UpdateUI();
        return this.Parent;
    }
}

public enum APlusBState
{
    EnterA,
    EnterB,
    Result
}

public class StepActions
{
    public Action UpdateUI { get; set; }

    public Func<ITerminalPresenter> Do1 { get; set; }

    public Func<ITerminalPresenter> Do2 { get; set; }
}

public class APlusBPresenter : TerminalPresenterBase
{
    private Int32 a, b;
    private APlusBState state;
    private String error = null;

    private Dictionary<APlusBState, StepActions> stateActions;

    private void InitializeStateActions()
    {
        this.stateActions = new Dictionary<APlusBState, StepActions>();

        this.stateActions.Add(APlusBState.EnterA,
            new StepActions()
            {
                UpdateUI = () =>
                {
                    this.view.Title = this.error ?? "Enter A";
                    this.view.Input = this.a.ToString();
                    this.view.Button1_Text = "Confirm A";
                    this.view.Button2_Text = "Exit";
                },
                Do1 = () => // Confirm A
                {
                    if (!Int32.TryParse(this.view.Input, out this.a))
                    {
                        this.error = "A is in incorrect format. Enter A again";
                        return this;
                    }

                    this.error = null;                     
                    this.state = APlusBState.EnterB;

                    return this;
                },
                Do2 = () => // Exit
                {
                    this.Reset();

                    return this.Parent;
                }
            });

        this.stateActions.Add(APlusBState.EnterB,
            new StepActions()
            {
                UpdateUI = () =>
                {
                    this.view.Title = this.error ?? "Enter B";
                    this.view.Input = this.b.ToString();
                    this.view.Button1_Text = "Confirm B";
                    this.view.Button2_Text = "Back to A";
                },
                Do1 = () => // Confirm B
                {
                    if (!Int32.TryParse(this.view.Input, out this.b))
                    {
                        this.error = "B is in incorrect format. Enter B again";
                        return this;
                    }

                    this.error = null;                     
                    this.state = APlusBState.Result;

                    return this;
                },
                Do2 = () => // Back to a
                {
                    this.state = APlusBState.EnterA;

                    return this;
                }
            });

        this.stateActions.Add(APlusBState.Result,
            new StepActions()
            {
                UpdateUI = () =>
                {
                    this.view.Title = String.Format("The result of {0} + {1}", this.a, this.b);
                    this.view.Output = (this.a + this.b).ToString();
                    this.view.Button1_Text = "Exit";
                    this.view.Button2_Text = "Back";
                },
                Do1 = () => // Exit
                {
                    this.Reset();

                    return this.Parent;
                },
                Do2 = () => // Back to B
                {
                    this.state = APlusBState.EnterB;

                    return this;
                }
            });
    }

    public APlusBPresenter(ITerminalView view) : base(view)
    {
        this.InitializeStateActions();
        this.Reset();
    }

    public override void UpdateUI()
    {
        this.view.Clear();

        this.stateActions[this.state].UpdateUI();
    }

    public override ITerminalPresenter this[int index]
    {
        get { throw new NotImplementedException(); }
    }

    public override ITerminalPresenter Do1()
    {
        var nextPresenter = this.stateActions[this.state].Do1();

        nextPresenter.UpdateUI();

        return nextPresenter;
    }

    public override ITerminalPresenter Do2()
    {
        var nextPresenter = this.stateActions[this.state].Do2();

        nextPresenter.UpdateUI();

        return nextPresenter;
    }

    public override void Reset()
    {
        this.state = APlusBState.EnterA;
        this.a = 0;
        this.b = 0;
        this.error = null;
    }
}

/// <summary>
/// Represents terminal presenter to use inside GUI. It handles current ISpecificTerminalPresenter inside itself.
/// </summary>
public class GlobalTerminalPresenter : IGlobalTerminalPresenter
{
    #region Fields

    private ITerminalPresenter current;
    private Int32 selectedIndex;

    #endregion


    #region Constructors

    public GlobalTerminalPresenter(ITerminalPresenter mainPresenter)
    {
        if (mainPresenter == null)
            throw new ArgumentNullException("mainPresenter");

        this.current = mainPresenter;

        this.UpdateUI();
    }

    #endregion

    public void UpdateUI()
    {
        this.current.UpdateUI();
    }

    public void Do1()
    {
        this.current = this.current.Do1();
    }

    public void Do2()
    {
        this.current = this.current.Do2();
    }

    public Int32 SelectedIndex
    {
        get
        {
            return this.selectedIndex;
        }
        set
        {
            this.selectedIndex = value;

            if (value == -1)
                return;

            this.current = this.current[value];
        }
    }

    public void Reset()
    {
        this.current.Reset();
    }
}

然后我们在表单的构造函数中初始化它们:

public partial class MainForm : Form,
    ITerminalView
{
    private IGlobalTerminalPresenter globalTerminalPresenter;

    public MainForm()
    {
        InitializeComponent();

        var nextLevelPresenters = new KeyValuePair<String, ITerminalPresenter>[]
        {
            new KeyValuePair<String, ITerminalPresenter>(
                "A plus B", 
                new APlusBPresenter(this)),
            new KeyValuePair<String, ITerminalPresenter>(
                "Just empty selector", 
                new SelectOptionPresenter(this, 
                    "Selector with no selection choices", 
                    Enumerable
                        .Empty<KeyValuePair<String, ITerminalPresenter>>()
                        .ToArray()))
        };

        var topPresenter = new SelectOptionPresenter(this, "Select the option and press the confirm button",  nextLevelPresenters);

        this.globalTerminalPresenter = new GlobalTerminalPresenter(topPresenter);
    }

P.S.1:这些代码片段假设您有一个名为MainForm的表单,它有两个按钮 - button1,button2,一个组合框,两个textBoxes - textBox_Input,textBox_Output。

P.S.2:使用的模式足够接近Model-View-Presenter,只有DataBindings

P.S.3 如果修改APlusBPresenter代码,则可以创建更多或更少的通用状态机Presenters。或者尝试塑造ChainXxxx...类和接口。

P.S.4:抱歉这些代码墙。对于[SO]格式来说,这可能太多了,所以我在GitHub上提供了特定的概念证明 - https://github.com/Podskal/StackOverflow_29870164.git。它在很多方面都很丑陋,但事实上,它至少可以提供关于如何实现自己的系统的一些想法。

P.S.5:此代码中存在许多有问题的地方,因此您应该仔细考虑如何从中构建自己的系统。