我正在使用checklistboxes来显示与表关联的FieldNames(另一个checklistbox)。动态生成多个checklistbox,并在检查其中的任何项时触发事件'OnCheckListBoxItemCheck'。这里检查的当前项目是从对象'sender'获得的。令人怀疑的是,我如何将从动态核对表框中检查的项目与从clbtables检查的项目相关联?请帮忙......
private void clbTables_ItemCheck(object sender, ItemCheckEventArgs e)
{
int indexofselectedtable;
indexofselectedtable = Convert.ToInt32(clbTables.SelectedIndex);
Metadata metadataobj = new Metadata(dbProperties);
List<string> ColumnNames = new List<string>();
ColumnNames = metadataobj.GetColumns(clbTables.Items[indexofselectedtable].ToString());
chklistcolumns = new CheckedListBox();
//Adds to the Checked ListBox
for (int j = 0; j < ColumnNames.Count; j++)
{
chklistcolumns.Items.Add(ColumnNames.ElementAt(j).ToString());
}
this.Controls.Add(chklistcolumns);
//To fire event for a dynamically generated column checklistboxes
chklistcolumns.ItemCheck += new ItemCheckEventHandler(OnCheckListBoxItemCheck);
}
private void OnCheckListBoxItemCheck(object sender, ItemCheckEventArgs args)
{
Columns columnobj = new Columns();
columnobj.ColumnName = this.chklistcolumns.SelectedItem.ToString();
columnobj.TableName = this.clbTables.SelectedItem.ToString();
selectedColumnsList.Add(columnobj);
}
答案 0 :(得分:1)
这可能是你想要的?
checkedListBox1.Items.Add("test");
checkedListBox1.ItemCheck += new ItemCheckEventHandler(checkedListBox1_ItemCheck);
....
void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
Console.WriteLine(((CheckedListBox)sender).Name + " is the father of item nr: " + e.Index);
Console.WriteLine("The value of element nr " + e.Index + " is " + ((CheckedListBox)sender).Items[e.Index].ToString());
}