C# 动态创建的Winform组合框都选择相同的值

C# 动态创建的Winform组合框都选择相同的值,c#,combobox,dynamically-generated,bindinglist,C#,Combobox,Dynamically Generated,Bindinglist,我正在用C#和VS 2010 Ultimate创建一个winform应用程序。我正在用动态创建的组合框填充flowlayoutpanel,所有组合框都数据绑定到同一个bindinglist。 当我运行应用程序时,它会正确地添加组合框,但是当我在一个组合框上选择一个项目时,所有其他项目都会使用相同的选择进行更新。 我做错了什么? 非常感谢您的帮助。 J 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用System.Lin

我正在用C#和VS 2010 Ultimate创建一个winform应用程序。我正在用动态创建的组合框填充flowlayoutpanel,所有组合框都数据绑定到同一个bindinglist。 当我运行应用程序时,它会正确地添加组合框,但是当我在一个组合框上选择一个项目时,所有其他项目都会使用相同的选择进行更新。 我做错了什么? 非常感谢您的帮助。 J

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
命名空间TestCompleteForm
{
公共部分类Form1:Form
{
private int comboBoxIndex=0;
列出现有文件;
BindingList现有系统列表;
列出所选文件;
BindingList SelectedSystemsList;
BindingList ListOfLanguages=新建BindingList();
BindingList ListOfSQLServers=新建BindingList();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
DirectoryInfo dinfo=新的DirectoryInfo(@“C:\Hosts”);
FileInfo[]Files=dinfo.GetFiles(“*.txt”);
Existingfiles=新列表();
foreach(文件中的文件信息文件)
{
Existingfiles.Add(Path.GetFileNameWithoutExtension(file.Name));
}
ExistingSystemsList=新绑定列表(Existingfiles);
lbAvailableSystems.DataSource=现有系统列表;
Selectedfiles=新列表();
SelectedSystemsList=新绑定列表(Selectedfiles);
lbSelectedSystems.DataSource=SelectedSystemsList;
//创建语言列表
var txtLanguages=File.ReadAllLines(@“C:\Languages\Languages.txt”);
foreach(txtLanguages中的vars)语言列表;
//创建sql Server的列表
var txtSqlServers=File.ReadAllLines(@“C:\SQL Server\SQL Server.txt”);
foreach(txtSqlServers中的var s)ListOfSQLServers.Add;
}
私有无效选项卡控件1\u单击(对象发送方,事件参数e)
{
if(tabControl1.SelectedTab.Name.ToString()=“第2页”)
{
而(flowLayoutPanel1.Controls.Count>0)
{
var ControlToRemove=flowLayoutPanel1.Controls[0];
FlowLayoutPanel 1.控件。移除(控件移除);
ControlToRemove.Dispose();
}
//flowLayoutPanel1.Controls.Clear();
foreach(SelectedSystemsList中的字符串StrSystem)
{
Label lNewSystem=新标签();
lNewSystem.Text=StrSystem;
flowLayoutPanel1.Controls.Add(lNewSystem);
//添加语言的组合框
ComboBox cbLanguages=新ComboBox();
cbLanguages.Name=“cbLanguages”+comboBoxIndex.ToString();
cbLanguages.DataSource=语言列表;
flowLayoutPanel1.Controls.Add(cbLanguages);
//为数据库服务器添加组合框
ComboBox cbSqlServers=新建ComboBox();
cbSqlServers.Name=“cbSqlServers”+comboBoxIndex.ToString();
cbSqlServers.DataSource=ListOfSQLServers;
flowLayoutPanel1.Controls.Add(cbSqlServers);
comboBoxIndex++;
}
}
}

忽略bindinglist,使用ComboBox.Items.Add()将项添加到控件中。

解决了这个问题。问题出在bindinglist上。由于某些原因,在创建动态控件时,我无法使用bindinglist。我只是在列表中循环使用ComboBox.Items.Add()现在可以用了。谢谢!你让我开心了。我用
String[]
作为数据源,所以我以
cmbNew.DataSource=arr.ToArray();
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace TestCompleteForm
{
public partial class Form1 : Form
{
    private int comboBoxIndex = 0;
    List<string> Existingfiles;
    BindingList<string> ExistingSystemsList;
    List<string> Selectedfiles;
    BindingList<string> SelectedSystemsList;
    BindingList<string> ListOfLanguages = new BindingList<string>();
    BindingList<string> ListOfSQLServers = new BindingList<string>();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Hosts");
        FileInfo[] Files = dinfo.GetFiles("*.txt");
        Existingfiles = new List<string>();

        foreach (FileInfo file in Files)
        {
            Existingfiles.Add(Path.GetFileNameWithoutExtension(file.Name));
        }
        ExistingSystemsList = new BindingList<string>(Existingfiles);
        lbAvailableSystems.DataSource = ExistingSystemsList;


        Selectedfiles = new List<string>();
        SelectedSystemsList = new BindingList<string>(Selectedfiles);
        lbSelectedSystems.DataSource = SelectedSystemsList;


        //Creat list of languages
        var txtLanguages = File.ReadAllLines(@"C:\Languages\Languages.txt");
        foreach (var s in txtLanguages) ListOfLanguages.Add(s);

        //Creat list of sql servers
        var txtSqlServers = File.ReadAllLines(@"C:\SQL Servers\sql-servers.txt");
        foreach (var s in txtSqlServers) ListOfSQLServers.Add(s);
    }

    private void TabControl1_Click(object sender, EventArgs e)
    {
        if (tabControl1.SelectedTab.Name.ToString() == "page2")
        {
            while (flowLayoutPanel1.Controls.Count > 0)
            {
                var ControlToRemove = flowLayoutPanel1.Controls[0];
                flowLayoutPanel1.Controls.Remove(ControlToRemove);
                ControlToRemove.Dispose();
            }
            //flowLayoutPanel1.Controls.Clear();
            foreach (string StrSystem in SelectedSystemsList)
            {
                Label lNewSystem = new Label();
                lNewSystem.Text = StrSystem;
                flowLayoutPanel1.Controls.Add(lNewSystem);
                //Add combobox for languages
                ComboBox cbLanguages = new ComboBox();
                cbLanguages.Name = "cbLanguages" + comboBoxIndex.ToString();
                cbLanguages.DataSource = ListOfLanguages;
                flowLayoutPanel1.Controls.Add(cbLanguages);
                //Add combobox for database servers
                ComboBox cbSqlServers = new ComboBox();
                cbSqlServers.Name = "cbSqlServers" + comboBoxIndex.ToString();
                cbSqlServers.DataSource = ListOfSQLServers;
                flowLayoutPanel1.Controls.Add(cbSqlServers);
                comboBoxIndex++;
            }
        }
    }