如何从动态创建的组合框中读取选择?在c#

本文关键字:读取 选择 组合 动态 创建 | 更新日期: 2023-09-27 18:03:12

我试图在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;

提前感谢!

如何从动态创建的组合框中读取选择?在c#

通过列表管理添加的控件:

    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());

你需要像平常那样编写一个事件处理程序,例如

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

创建ComboBox控件时,将该方法附加到事件上,例如

var cbx = new ComboBox();
cbx.SelectedIndexChanged += ComboBoxes_SelectedIndexChanged;

在事件处理程序中,sender参数指引发事件的对象,即SelectedIndex刚刚发生变化的ComboBox,例如

private void ComboBoxes_SelectedIndexChanged(object sender, EventArgs e)
{
    var cbx = (ComboBox) sender;
    var selection = cbx.Text;
    MessageBox.Show(selection, "You chose...");
}