获取组合框项目标记

时间:2013-03-08 15:52:57

标签: c# winforms visual-studio-2010

对我收到此错误的原因感到困惑:

Combobox项:

private readonly string[] cbTexts = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "(Cleare)" };

动态comboboxes

private void comboOptions()
        {
            cmb = new ComboBox[countWaiters];
            int width = 100;
            int height = 20;
            for (int i = 0; i <= 3; ++i)
            {
                newBox = new ComboBox();         
                newBox.Text = "(Request)";          
                newBox.Size = new Size(width, height);
                newBox.Location = new Point(0, (i * height));
                cmb[i] = newBox;
                cmb[i].SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
                foreach (string cbText in cbTexts)
                {
                    cmb[i].Items.Add(cbText);
                }

                pnlRequests.Controls.Add(newBox);
            }            
        }
SelectedIndexChanged

comboboxes事件:

void cmb_SelectedIndexChanged(object sender, EventArgs e)
        {

            ComboBox senderCmb = (ComboBox)sender;

           int row = (int)senderCmb.Tag;
           //this is where the program crashes
           //more code located here that does something
         }

错误: 注意:某些方法名称已从此图像更改,但不会产生任何影响 enter image description here

2 个答案:

答案 0 :(得分:2)

您没有在给定代码中的任何位置设置Tag属性,我无法理解您需要的原因。我怀疑你想得到当前的SelectedIndex

int row = senderCmb.SelectedIndex;

答案 1 :(得分:0)

我错过了一句话,愚蠢的错误:

cmb[i].Tag = i;