下拉列表选中更改事件触发按钮事件

时间:2018-03-08 07:09:37

标签: c# windows winforms

我在seasoncategoryGenderdivision使用了4个下拉列表。基于Categorygenderdivision的验证,至少需要选择一个。

private void cmbpastseason_SelectedIndexChanged(object sender, EventArgs e)
{
    if (validateComboBox())
    {
        //code snippet
    }

}

private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    if (validateComboBox())
}

private void comboBoxDept_SelectedIndexChanged(object sender, EventArgs e)
{
    if (validateComboBox())
        //code snippet
}

private void comboBoxDivision_SelectedIndexChanged(object sender, EventArgs e)
{
    if (validateComboBox())
        //code snippet
}

private bool validateComboBox()
{
    bool isValid = false;
    try
    {
        if (!string.IsNullOrEmpty(comboBoxCategory.Text))
            isValid = true;
        if (!string.IsNullOrEmpty(comboBoxDept.Text))
            isValid = true;
        if (!string.IsNullOrEmpty(comboBoxDivision.Text))
            isValid = true;
        if (!isValid)
            MessageBox.Show("Select any one of the category, Gender and 
                Division ");
    }}
    catch (Exception ex)
    {

    }
    return isValid;
}

在我点击页面上的其他按钮后选择它会触发事件。

2 个答案:

答案 0 :(得分:0)

请尝试此代码

方法1:

bool isAnySelected=false;

public Form1()
{
    cmbpastseason.SelectedIndexChanged += CommonHandler;
    comboBoxCategory.SelectedIndexChanged += CommonHandler;
    comboBoxDept.SelectedIndexChanged += CommonHandler;
    comboBoxDivision.SelectedIndexChanged += CommonHandler;
}

private void CommonHandler(object sender, EventArgs e)
{
    validateComboBox();
}

private void validateComboBox()
{
    if(!string.IsNullOrWhiteSpace(comboBoxDept.Text) || 
        !string.IsNullOrWhiteSpace(comboBoxCategory.Text) ||
        !string.IsNullOrWhiteSpace(comboBoxDept.Text) ||
        !string.IsNullOrWhiteSpace(comboBoxDivision.Text))
        {
            isAnySelected = true;
        }
    else
        isAnySelected = false;
}

方法2:

bool isAnySelected=false;

private void cmbpastseason_SelectedIndexChanged(object sender, EventArgs e)
{
    validateComboBox();
}

private void comboBoxCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    validateComboBox();
}

private void comboBoxDept_SelectedIndexChanged(object sender, EventArgs e)
{
    validateComboBox();
}

private void comboBoxDivision_SelectedIndexChanged(object sender, EventArgs e)
{
    validateComboBox();
}

private void validateComboBox()
{
    if(!string.IsNullOrWhiteSpace(cmbpastseason.Text) || 
        !string.IsNullOrWhiteSpace(comboBoxCategory.Text) ||
        !string.IsNullOrWhiteSpace(comboBoxDept.Text) ||
        !string.IsNullOrWhiteSpace(comboBoxDivision.Text))
        {
            isAnySelected = true;
        }
    else
    {
        isAnySelected = false;
        MessageBox.Show("Nothing Selected");
    }
}

您的按钮点击事件如下所示

private void button1_Click(object sender, EventArgs e)
{
    if (isAnySelected)
        MessageBox.Show("There is some selection");
    else
        MessageBox.Show("Nothing Selected");
}

答案 1 :(得分:0)

加载页面时放在代码下方

      ListBox:
      ListBox cb = (ListBox)sender;
        if (!cb.Focused)
           return;
      Combox:
        combobox cb = (combobox)sender;
        if (!cb.Focused)
           return;