簡體   English   中英

ComboBox SelectedIndexChanged 事件未觸發

[英]ComboBox SelectedIndexChanged event not firing

我將 VS 2017 用於從主窗體調用窗體的 Visual C# 應用程序(.Net 4.6.2,32 位)。 在第二種形式中, SelectedIndexChanged事件不會為ComboBoxes之一觸發。 以下是代碼。 如果我必須注冊該事件,我不知道如何進行。 我最初將ComboBoxes復制並粘貼到表單上。 然后我刪除了該控件並從工具箱中重新添加了ComboBoxes 任何幫助,將不勝感激。

namespace Lottery_C_Sharp
{
    public partial class Dialog_Matches_Input_Lotomania : Form
    {
        MatchesMethods_LM m;
        public string[] lotomania_list = new string[10];
        public string[] pick10_list = new string[5];
        Utilities u;

        public Dialog_Matches_Input_Lotomania(MatchesMethods_LM mm)
        {
            InitializeComponent();

            m = mm;
            u = new Utilities();
            set_combos(); 

            comboBox1.SelectedIndex = 0;
            comboBox2.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
        }
        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("comboBox3_SelectedIndexChanged");

            if (m.NumCurrLimit == 99)
            {
                set_lotomania_time_text();
                set_lotomania_totals_text();
            }
            else
            {
                set_pick10_time_text();
                set_pick10_totals_text();
            }
        }

        public void set_combos()
        {
            set_lists();

            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();

            if (m.NumCurrLimit == 99)
            {
                textBox1.Text = "Brazilian LotoMania";

                AddToCombo(comboBox1, lotomania_list);
                comboBox1.SelectedIndex = 0;

                AddToCombo(comboBox2, lotomania_list);
                comboBox2.SelectedIndex = 0;

                AddToCombo(comboBox3, lotomania_list);
                comboBox3.SelectedIndex = 0;

                set_lotomania_time_text();
                set_lotomania_totals_text();

            }
            else
            {
                textBox1.Text = "USA New York Pick 10";

                AddToCombo(comboBox1, pick10_list);
                comboBox1.SelectedIndex = 0;

                AddToCombo(comboBox2, pick10_list);
                comboBox2.SelectedIndex = 0;

                AddToCombo(comboBox3, pick10_list);
                comboBox3.SelectedIndex = 0;

                set_pick10_time_text();
                set_pick10_totals_text();
            }
        }

您有多種選擇來注冊事件。

選項1:

在代碼中注冊:

public Dialog_Matches_Input_Lotomania(MatchesMethods_LM mm)
{
    InitializeComponent();

    ...

    comboBox1.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}

選項 2:

在設計器中雙擊comboBox1 ,這將自動添加和注冊一個事件。

選項 3:

選擇設計器中的comboBox1 ,右鍵選擇“屬性”,在屬性窗口中選擇最上方的事件圖標(閃電符號),找到“SelectedIndexChanged”輸入事件名稱或雙擊自動添加注冊一個事件。

您應該在 Designer 屬性中為您想要的組合框注冊事件,如下所示: 如何在 WinForms 設計器中注冊事件

它在Form1.Designer.cs (表單名稱取決於您如何命名)自動生成的文件中生成事件注冊:

this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);

然后你的函數,當事件發生時你可以執行一些操作顯示在Form1.cs

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    // Do something
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM