从组合框中分配不同的词典

时间:2015-03-23 17:10:47

标签: c# dictionary combobox decision-tree linear

我有一个线性决策树,用户可以在其中选择不同的组合框,这些组合框在他们收到的表格中创建不同的可能结果。在这种情况下,我需要创造一个结果的两个因素。根据textbox3中输入的数字和combobox3的选择,用户可以在textbox1中指定的目录中接收不同的表单。

我绝对不是一个强大的C#程序员,但是,我觉得大部分都是相当合理的,因为唯一不起作用的项目是combobox3.Text和Textbox.3字典之间的链接。

 public Form(){

    InitializeComponent();
    button2.Click += mainPageRule;
    button2.Click += mainPageCheck1;
    button2.Click += mainPageCheck2;
    button2.Click += mainPageSwitch;
    button2.Click += mainPageActivator;
}  
    protected void mainPageRule(object sender, EventArgs e){
    this.timer1.Start();
    string destination = textBox1.Text;
    string source = textBox2.Text;
    if (textBox1.Text == "")
    {
        MessageBox.Show("You forgot to set a destination!");
    }
    else if (mapping.ContainsKey(source))
    {
        string directoryName = mapping[source];
        foreach (var f in Directory.GetFiles(directoryName))
        {
            File.Copy(f, Path.Combine(destination, Path.GetFileName(f)));
            GC.Collect();
            GC.WaitForPendingFinalizers();
            progressBar1.Value = 0;
        }
    }
    else
    {
        MessageBox.Show("Oh No! The output code is incorrect or something went wrong! Please Try Again! -Love Joe");
    }
    if (progressBar1.Value == 100)
    {
        this.timer1.Stop();
        progressBar1.Value = 0;
    }}
protected void mainPageSwitch(object sender, EventArgs e){
    if (comboBox3.Text == "Hybrid")
    { 
        string payout = "hybridMain";
    }
    else if (comboBox3.Text == "Corporate")
    {
        string payout = "swagMain";
    }
    else 
    {
        MessageBox.Show("You Forgot to set your platform");
    }}
protected void mainPageActivator(object sender, EventArgs e){
    string destination = textBox1.Text;
    string source = textBox3.Text;

    if (payout.ContainsKey(source))
    {
        string directoryName = payout[source];
        foreach (var f in Directory.GetFiles(directoryName));
        }}
        private Dictionary<string, string> hybridMain = new Dictionary<string, string>{
             //THIS SECTION CALLS TO 1KN3 NEW ADVISORS
             { "0000", @"\\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NC0"},
         };
private Dictionary<string, string> mapping = new Dictionary<string, string>{
             //THIS SECTION CALLS TO 1KN3 NEW ADVISORS
             { "0000", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NC0"},
             { "0001", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NCRA" },
             { "0002", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NCUA" },
             { "0003", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NCB" },
             { "0010", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NH0" },
             { "0011", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NHRA" },
             { "0012", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NHUA" },
             { "0013", @"\\FSN-SERVER\Server\A.L.E.X.A DATABASE\1KN3\New Advisor\NHB" },
         }

1 个答案:

答案 0 :(得分:2)

不应该在一个处理程序上触发所有事件,而应该将这些事件分解为使特定控件触发的事件最合适的更改事件处理程序。即ComboBox.OnSelectedIndexChanged或其中任何一个。当每个控件改变时,下一个效果将级联直到完成。

以特定顺序将多个事件方法分配给处理程序并不保证它们按该顺序执行。你可以更好地重构,以便更具体地执行你的执行顺序。

另一种方法是在每个先前排序的方法的末尾链接方法调用,这样可以保证所有事件按顺序触发,如果你只需要点击按钮就可以了。

增加:

我的意思是,如果所有这些方法都需要按特定顺序可靠地执行,那么您应该具有类似以下内容:

public partial class TestForm : Form
{
    public TestForm()
    {
        btnSubmit.Click += new System.EventHandler(button_click);
    }
    public void button_click(object sender, EventArgs e)
    {
        // do some code
        SecondMethod();
    }
    public void SecondMethod()
    {
        // Do some more code that has to wait until first method is done.
        ThirdMethod()
    }
    public void ThirdMethod()
    {
        // Do your final code.
    }
}