gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:02:53 25 4
gpt4 key购买 nike

我正在使用 C# 和 VS 2010 Ultimate 创建一个 winform 应用程序。我正在使用动态创建的组合框填充流程布局面板,所有数据都绑定(bind)到同一个绑定(bind)列表。当我运行该应用程序时,它会正确添加组合框,但是当我在一个组合框中选择一个项目时,所有其他组合框都会更新为相同的选择。我究竟做错了什么?非常感谢您收到的任何帮助。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++;
}
}
}

最佳答案

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

关于c# - 动态创建的 Winform 组合框都选择相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com