1

CheckedListBox以下からプログラムで作成したコントロールがあります...

Button btnSelectAll = new Button();
btnSelectAll.Text = "Select All";
btnSelectAll.Name = item.Id;
btnSelectAll.Tag = param.Id;
CheckedListBox chkListBox = new CheckedListBox();
chkListBox.Size = new System.Drawing.Size(flowPanel.Size.Width - lblListBox.Size.Width - 10, 100);
//set the name and tag for downstream event handling since two-way bindings are not possible with control
chkListBox.Tag = param.Id;
chkListBox.Name = item.Id;
chkListBox.ItemCheck += new ItemCheckEventHandler(chkListBox_ItemCheck);
btnSelectAll.Click += new EventHandler(btnSelectAll_Click);

項目を動的に作成したときに、chkListBox の ItemCheck がヒットするたびにイベント ハンドラーも追加したことに注意してください。コードのどこかで...私は...

CheckedListBox tmpCheckedListBox = cntrl as CheckedListBox;
for (int i = 0; i < tmpCheckedListBox.Items.Count; i++)
{
   tmpCheckedListBox.SetItemChecked(i, true);
}

上記を実行すると、ItemChecked イベントが発生しません。ユーザーがアイテムをクリックしたかのように、このイベントを発生させるにはどうすればよいですか?

4

1 に答える 1

2

One way is to just call the same method as assigned to the event and pass in the correct control for sender, for example:

for (int i = 0; i < tmpCheckedListBox.Items.Count; i++)
{
   tmpCheckedListBox.SetItemChecked(i, true);
   chkListBox_ItemCheck(tmpCheckedListBox.Items[i],null);
}

You can usually get away with passing EventArgs.Empty or null as the event arguments, however if you depend on them in the event handler you would need to construct the correct arguments class and pass that in, for example:

for (int i = 0; i < tmpCheckedListBox.Items.Count; i++)
{
   var args = new ItemCheckEventArgs(i,true,tmpCheckedListBox.GetItemChecked(i));

   tmpCheckedListBox.SetItemChecked(i, true);
   chkListBox_ItemCheck(tmpCheckedListBox.Items[i],args);
}
于 2013-03-19T22:25:23.433 に答える