单击ComboBox和按钮时,不会执行操作



我是全新的形式,我有问题。我想选择Combobox中的一个项目,然后点击按钮,然后执行有关选择的项目的操作。我正在创建带有选项的列表,布尔值以检查按钮是否被点击和整数索引。

List<string> options = new List<string> {"Dodaj studenta", "Wyświetl studenta", "Edytuj studenta" };
private bool button1WasClicked = false;
int index;

我正在尝试从Combobox读取索引:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    index = comboBox1.SelectedIndex;
}

将句柄设置为按钮,以将布尔值的值更改为true当用户命中按钮时:

private void button1_Click(object sender, EventArgs e)
{
    button1WasClicked = true;
}

并设置ComboBox:

private void comboBoxSetup()
{ 
    this.comboBox1.DataSource = options;
    this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList; //read only
    if(index == 0 && button1WasClicked == true)
    {
        System.Windows.Forms.MessageBox.Show("My message here");
    }
 }

ps:在构造函数中,i有compoboxSetup();:)

当我仅检查条件的索引时 - 可见弹出窗口。感谢提前的任何帮助!

感谢@plutonix有效的解决方案:

在构造函数中调用comboboxSetup()我将整个代码从此方法移动到

button1_click(对象发送者,EventArgs E)

最新更新