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

时间:2013-04-15 14:59:06

标签: c# combobox dynamically-generated bindinglist

我正在用C#和VS 2010 Ultimate创建一个winform应用程序。我正在使用动态创建的combox填充flowlayoutpanel,所有这些都被数​​据绑定到相同的绑定列表。 当我运行应用程序时,它会正确添加combox,但是当我在一个组合框中选择一个项目时,所有其他项目都会使用相同的选项进行更新。 我究竟做错了什么? 非常感谢您收到的任何帮助。 学家

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++;
            }
        }
    }

1 个答案:

答案 0 :(得分:6)

忽略绑定列表并使用ComboBox.Items.Add()代替将项添加到控件中。