[英]Checkbox event on IEnumerable box
我正在制作一個 c# windows 表單,從 IEnumerable 項目列表必須為每個項目創建一個復選框和一個文本框,到目前為止我已經毫無問題地完成了。 每個文本框和每個復選框的名稱都與旋轉的名稱+計數器不同。
我想做一個事件,如果我取消選中 Chkbox1,則 txtbox1 在不知道我可以擁有多少復選框的情況下不可編輯。
我是 c# 的新手。
使用Dictionary
來存儲CheckBox
和TextBox
之間的關系。 tag
屬性也可以做到這一點,但我認為它很難看。
//make the dict global, so can access in event handler
private Dictionary<CheckBox, TextBox> chkTbRelations;
private void Form1_Load(object sender, EventArgs e)
{
chkTbRelations = new Dictionary<CheckBox, TextBox>();
//simulate your IEnumerable
foreach (int i in Enumerable.Range(1, 5))
{
CheckBox chk = new CheckBox();
TextBox tb = new TextBox();
//add other properties such as Size, Location here
// Controls.Add ...
// tb.Tag = chk;
chkTbRelations.Add(chk, tb);
chk.CheckedChanged += Chk_CheckedChanged;
}
}
private void Chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = sender as CheckBox;
TextBox tb = chkTbRelations[chk];
//tb = chk.Tag as TextBox;
tb.Enabled = chk.Checked;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.