【发布时间】:2022-01-12 19:07:45
【问题描述】:
我有 2 个最初选中的复选框列表,我需要知道用户是否未选中 any 复选框(左侧),以便在不循环的情况下更新右侧的相应复选框状态每次用户单击时复选框的更改状态:
这是我目前创建列表的解决方案:
List<string> statuts_gauche = new List<string> { "005", "006" , "017" , "030", "031", "033", "035", "036", "026", "009", "022", "328", "329"};
List<string> statuts_droite = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("hello");
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
CheckBox box= (CheckBox)c;
Console.WriteLine("hh - " + box.Text + " : " + box.Checked );
}
}
}
private void Form1_Shown(object sender, EventArgs e)
{
Create_Checkboxes_ByTag("statuts_gauche",40);
statuts_droite = statuts_gauche;
Create_Checkboxes_ByTag("statuts_droite", 300);
}
private void Create_Checkboxes_ByTag(String Tag,int initialOffset,int y_position = 30) {
CheckBox box;
for (int i = 0; i < statuts_gauche.Count; i++)
{
box = new CheckBox();
box.Tag = Tag;
box.Text = statuts_gauche[i].ToString();
box.AutoSize = true;
box.Checked = true;
box.Location = new Point(initialOffset, y_position);
y_position = y_position + 40;
this.Controls.Add(box);
}
}
【问题讨论】:
-
我发布完答案后,您没有额外的实施细节。在您的
Create_Checkboxes_ByTag函数中,为您的复选框复选框指定一个唯一名称。这允许您执行this.Controls["nameofcheckbox"],然后只需确保您的名字是您可以轻松获取左侧复选框的名称并将其转换为右侧复选框的名称。