0

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

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
         }

错误:注意:此图像中的某些方法名称已更改,但没有任何影响 在此处输入图像描述

4

2 回答 2

2

您尚未Tag在给定代码中的任何位置设置该属性,我不明白您为什么需要这样做。我怀疑你想得到电流SelectedIndex

int row = senderCmb.SelectedIndex;
于 2013-03-08T15:58:30.550 回答
0

我错过了一条线,犯了愚蠢的错误:

cmb[i].Tag = i;
于 2013-03-08T16:01:48.440 回答