2

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

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

1 回答 1

7

忽略绑定列表并改用 ComboBox.Items.Add() 将项目添加到控件。

于 2013-04-16T16:03:47.617 回答