编程语言
首页 > 编程语言> > c#-根据系统状态以不同方式处理控制事件

c#-根据系统状态以不同方式处理控制事件

作者:互联网

我正在尝试构建Ingenico POS终端(iWL220)的模拟器.
主屏幕上有一个组合框.用户输入ID和密码后,组合框将加载6个菜单.如果用户单击btn1,则组合框清除菜单并添加另一组菜单.如果用户单击btn1作为新加载的菜单,则再次清除组合框并加载另一组菜单,依此类推.

我的问题是每个按钮单击(btn1,btn2,btn3,btn4,btn5),我必须编写很多if else语句.例;

第一个菜单(在组合框上)有6个扇区.

> 1.部门
> 2.扇区B
> 3.扇区C
> 4.扇区
> 5.部门
> 6.扇区

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

> 1.公司A
> 2.公司B
> 3.公司

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

> 1.全额付款
> 2.ParitalPayment

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

我已经将Ingenico终端图片加载为jpeg,并在其顶部设置了按钮.
我只给出了模拟的小版本.在我的应用中,用户可以选择114种可能性.

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

我的问题是,对于这114个概率案例,构造我的按钮事件的最短方法是什么?

我有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.该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”进行计算.

首先,您有5个(或多或少)菜单项,并且每次按任意(数字)按钮(在pos终端中为1到9)都将在屏幕上显示新菜单.

解决方法:

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

因此,如果我们将某个特定类(接口)中每个可能状态(状态序列)的当前动作封装起来,该怎么办:

/// <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,一个组合框,两个textBox-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:此代码中有很多有问题的地方,因此您应该非常仔细地考虑如何从中构建自己的系统.

标签:simulator,simulation,c,winforms
来源: https://codeday.me/bug/20191120/2043866.html