【问题标题】:Creating a void for the event "SelectionChangeCommitted" for a ComboBox array为 ComboBox 数组的“SelectionChangeCommitted”事件创建一个 void
【发布时间】:2014-06-22 17:23:35
【问题描述】:

我得到了一个包含 5 个组合框的数组。我想为整个数组的“SelectionChangeCommitted”事件创建一个 void,它可以确定从哪个组合框调用 void。这是包含 5 个空格的当前代码。每个组合框一个。

private ComboBox[] statsValues;

public frmMain()
    {
        InitializeComponent();
        statsValues = new ComboBox[5];
        for (byte b = 0; b < statsValues.Length; b++)
        {
            statsValues[b] = new ComboBox();
            statsValues[b].Location = new System.Drawing.Point(69, 193 + 30 * b);
            statsValues[b].DropDownStyle = ComboBoxStyle.DropDownList;
        }
        Controls.AddRange(statsValues);
        statsValues[0].SelectionChangeCommitted += new System.EventHandler(cmbSTR_SelectionChangeCommitted);
        statsValues[1].SelectionChangeCommitted += new System.EventHandler(cmbDEX_SelectionChangeCommitted);
        statsValues[2].SelectionChangeCommitted += new System.EventHandler(cmbCON_SelectionChangeCommitted);
        statsValues[3].SelectionChangeCommitted += new System.EventHandler(cmbINT_SelectionChangeCommitted);
        statsValues[4].SelectionChangeCommitted += new System.EventHandler(cmbWIS_SelectionChangeCommitted);
    }

    private void cmbSTR_SelectionChangeCommitted(object sender, EventArgs e)
    {
        //Code...
    }

    private void cmbDEX_SelectionChangeCommitted(object sender, EventArgs e)
    {
        //Code...
    }

    private void cmbCON_SelectionChangeCommitted(object sender, EventArgs e)
    {
        //Code...
    }

    private void cmbINT_SelectionChangeCommitted(object sender, EventArgs e)
    {
        //Code...
    }

    private void cmbWIS_SelectionChangeCommitted(object sender, EventArgs e)
    {
        //Code...
    }

我想为它们创建一个单独的 void,它可以确定从哪个组合框调用它。

【问题讨论】:

  • 我想为事件创建一个空虚......它可以确定从哪个组合框调用空虚 ??
  • 我认为你的意思是写“事件处理程序”而不是 void/emptiness。
  • 我疯了 xD 而且是的,哈哈。

标签: c# arrays combobox void eventhandler


【解决方案1】:

假设'a void'表示'事件处理方法',

private void cmbALL_SelectionChangeCommitted(object sender, EventArgs e)
{
    ComboBox thisOne = (ComboBox)sender;
    //Code...
}

【讨论】:

  • 那么将所有组合框绑定到同一个 void?
  • statsValues[x].SelectionChangeCommitted += cmbALL_SelectionChangeCommitted;
  • 是的,它与 if 语句完美配合,检查发件人 (ComboBox) 是否等于组合框之一!而且我还可以在 for 循环中包含事件处理程序;)谢谢伙计们:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多