将 CheckedListBox ItemCheck 事件管理到 运行 在检查项目之后而不是之前
Manage CheckedListBox ItemCheck event to run after an item checked not before
我在 C# Window 表单应用程序中使用 CheckedListBox
。
我想在一项选中或取消选中后执行某些操作,但 ItemCheck
事件在该项 checked/unchecked 之前运行。
我该怎么做?
您可以在 ItemCheck 上连接一个事件。您可以通过右键单击您的复选框列表和 select 属性来完成。在右侧你会看到 属性 选项卡,单击事件选项卡按钮并找到 ItemCheck 事件并双击它。它将根据您的复选框列表名称为您生成一个事件方法,如下所示。
然后,您可以使用下面的代码验证 selected/checked 复选框。
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
var checkBoxName = checkedListBox1.Items[e.Index];
Console.WriteLine("Current {0}, New {1} , value {2}", e.CurrentValue, e.NewValue, checkBoxName);
}
CheckedListBox.ItemCheck Event
The check state is not updated until after the ItemCheck event occurs.
对于运行项目检查后的一些代码,您应该使用变通方法。
最佳选择
您可以使用此选项(感谢 Hans Passant for this post):
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
this.BeginInvoke(new Action(() =>
{
//Do the after check tasks here
}));
}
另一种选择
如果在 ItemCheck 事件中,你需要知道项目的状态,你应该使用 e.NewValue
而不是 checkedListBox1.GetItemChecked(i)
如果您需要将检查索引列表传递给方法,请执行以下操作:
使用代码:
var checkedIndices = this.checkedListBox1.CheckedIndices.Cast<int>().ToList();
if (e.NewValue == CheckState.Checked)
checkedIndices.Add(e.Index);
else
if(checkedIndices.Contains(e.Index))
checkedIndices.Remove(e.Index);
//now you can do what you need to checkedIndices
//Here if after check but you should use the local variable checkedIndices
//to find checked indices
另一种选择
在 ItemCheck 事件中间,删除 ItemCheck 的处理程序、SetItemCheckState,然后重新添加处理程序。
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
var control = (CheckedListBox)sender;
// Remove handler
control.ItemCheck -= checkedListBox_ItemCheck;
control.SetItemCheckState(e.Index, e.NewValue);
// Add handler again
control.ItemCheck += checkedListBox_ItemCheck;
//Here is After Check, do additional stuff here
}
尝试搜索更多答案,因为这里是
private void clbOrg_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox clb = (CheckedListBox)sender;
// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;
// Now you can go further
CallExternalRoutine();
}
以及 link:
Which CheckedListBox event triggers after a item is checked?
我在 C# Window 表单应用程序中使用 CheckedListBox
。
我想在一项选中或取消选中后执行某些操作,但 ItemCheck
事件在该项 checked/unchecked 之前运行。
我该怎么做?
您可以在 ItemCheck 上连接一个事件。您可以通过右键单击您的复选框列表和 select 属性来完成。在右侧你会看到 属性 选项卡,单击事件选项卡按钮并找到 ItemCheck 事件并双击它。它将根据您的复选框列表名称为您生成一个事件方法,如下所示。
然后,您可以使用下面的代码验证 selected/checked 复选框。
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
var checkBoxName = checkedListBox1.Items[e.Index];
Console.WriteLine("Current {0}, New {1} , value {2}", e.CurrentValue, e.NewValue, checkBoxName);
}
CheckedListBox.ItemCheck Event
The check state is not updated until after the ItemCheck event occurs.
对于运行项目检查后的一些代码,您应该使用变通方法。
最佳选择
您可以使用此选项(感谢 Hans Passant for this post):
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
this.BeginInvoke(new Action(() =>
{
//Do the after check tasks here
}));
}
另一种选择
如果在 ItemCheck 事件中,你需要知道项目的状态,你应该使用
e.NewValue
而不是checkedListBox1.GetItemChecked(i)
如果您需要将检查索引列表传递给方法,请执行以下操作:
使用代码:
var checkedIndices = this.checkedListBox1.CheckedIndices.Cast<int>().ToList();
if (e.NewValue == CheckState.Checked)
checkedIndices.Add(e.Index);
else
if(checkedIndices.Contains(e.Index))
checkedIndices.Remove(e.Index);
//now you can do what you need to checkedIndices
//Here if after check but you should use the local variable checkedIndices
//to find checked indices
另一种选择
在 ItemCheck 事件中间,删除 ItemCheck 的处理程序、SetItemCheckState,然后重新添加处理程序。
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
var control = (CheckedListBox)sender;
// Remove handler
control.ItemCheck -= checkedListBox_ItemCheck;
control.SetItemCheckState(e.Index, e.NewValue);
// Add handler again
control.ItemCheck += checkedListBox_ItemCheck;
//Here is After Check, do additional stuff here
}
尝试搜索更多答案,因为这里是
private void clbOrg_ItemCheck(object sender, ItemCheckEventArgs e)
{
CheckedListBox clb = (CheckedListBox)sender;
// Switch off event handler
clb.ItemCheck -= clbOrg_ItemCheck;
clb.SetItemCheckState(e.Index, e.NewValue);
// Switch on event handler
clb.ItemCheck += clbOrg_ItemCheck;
// Now you can go further
CallExternalRoutine();
}
以及 link: Which CheckedListBox event triggers after a item is checked?