我正在尝试在C#中创建一个表单应用程序,它将创建几个组合框,用可选项填充它们,然后能够检测选择了哪些项。我有前两个部分工作:我可以创建组合框,添加项目,但当我尝试编写代码来读取已选择的项目时,我得到错误,因为控件在构建时不存在..
public System.Windows.Forms.ComboBox AddNewSEPComboBox()
{
System.Windows.Forms.ComboBox SEPcbox = new System.Windows.Forms.ComboBox();
this.Controls.Add(SEPcbox);
SEPcbox.Top = A * 28;
SEPcbox.Left = 250;
string[] SEPSTAT = new string[]{"current (12.1.4013.4013), no changes made.", "not installed, no changes made.", "not installed, latest version (12.1.4013.4013) installed.", "outdated, updated to the latest version (12.1.4013.4013).", "outdated, no changes made."};
SEPcbox.Items.AddRange(SEPSTAT);
SEPcbox.Name = "SEPcbox" + A;
A++;
return SEPcbox;
提前致谢!
答案 0 :(得分:0)
您需要像往常一样编写事件处理程序,例如
private void ComboBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
// ...
}
创建ComboBox
控件时,将该方法附加到事件,例如
var cbx = new ComboBox();
cbx.SelectedIndexChanged += ComboBoxes_SelectedIndexChanged;
在事件处理程序中,sender
参数引用引发事件的对象,即ComboBox
刚刚更改的SelectedIndex
,例如。
private void ComboBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
var cbx = (ComboBox) sender;
var selection = cbx.Text;
MessageBox.Show(selection, "You chose...");
}
答案 1 :(得分:0)
按列表管理您添加的控件:
List<System.Windows.Forms.ComboBox> lstComboBoxAdded = new List<System.Windows.Forms.ComboBox>();
public System.Windows.Forms.ComboBox AddNewSEPComboBox()
{
System.Windows.Forms.ComboBox SEPcbox = new System.Windows.Forms.ComboBox();
SEPcbox.Top = A * 28;
SEPcbox.Left = 250;
SEPcbox.Location = new Point(20, A * 30);
string[] SEPSTAT = new string[] { "current (12.1.4013.4013), no changes made.", "not installed, no changes made.", "not installed, latest version (12.1.4013.4013) installed.", "outdated, updated to the latest version (12.1.4013.4013).", "outdated, no changes made." };
SEPcbox.Items.AddRange(SEPSTAT);
SEPcbox.Name = "SEPcbox" + A;
A++;
this.Controls.Add(SEPcbox);
lstComboBoxAdded.Add(SEPcbox);
return SEPcbox;
}
然后得到你的价值:
MessageBox.Show(lstComboBoxAdded.Where(m => m.Name == "SEPcbox" + A).First().Text);
或管理索引/值/文字:
Dictionary<string, int> dicIndexSelected = new Dictionary<string, int>();
public System.Windows.Forms.ComboBox AddNewSEPComboBox()
{
System.Windows.Forms.ComboBox SEPcbox = new System.Windows.Forms.ComboBox();
SEPcbox.Top = A * 28;
SEPcbox.Left = 250;
SEPcbox.Location = new Point(20, A * 30);
string[] SEPSTAT = new string[] { "current (12.1.4013.4013), no changes made.", "not installed, no changes made.", "not installed, latest version (12.1.4013.4013) installed.", "outdated, updated to the latest version (12.1.4013.4013).", "outdated, no changes made." };
SEPcbox.Items.AddRange(SEPSTAT);
SEPcbox.Name = "SEPcbox" + A;
A++;
this.Controls.Add(SEPcbox);
dicIndexSelected.Add(SEPcbox.Name, -1);
SEPcbox.SelectedIndexChanged += new EventHandler(SEPcbox_SelectedIndexChanged);
return SEPcbox;
}
void SEPcbox_SelectedIndexChanged(object sender, EventArgs e)
{
dicIndexSelected[((System.Windows.Forms.ComboBox)sender).Name] = ((System.Windows.Forms.ComboBox)sender).SelectedIndex;
}
然后得到你的价值:
MessageBox.Show(dicIndexSelected["SEPcbox" + A].ToString());