[英]How to disable check/uncheck of an item in a checkedlistbox in c#
I have a CheckedListBox with 10 items.我有一个包含 10 个项目的 CheckedListBox。 On each item check a method is being called.
在每个项目检查一个方法被调用。 I want to disable the checkbox of that particular item for which the method is being executed so that user cannot uncheck the item till the job is completed.
我想禁用正在执行该方法的特定项目的复选框,以便用户在作业完成之前无法取消选中该项目。
** Unchecking of an item calls another method. ** 取消选中一个项目会调用另一个方法。
Here is the code of ItemCheck Event这是ItemCheck事件的代码
private void host_listbox_ItemCheck(object sender, ItemCheckEventArgs e)
{
int index = e.Index;
try
{
string sitem = host_listbox.Items[index].ToString();
host_list[sitem].checked_event=e;
if (!host_list[sitem].is_busy)
{
host_config.listEnabled = false;
host_list[sitem].con_worker.RunWorkerAsync();
}
if (host_listbox.GetItemCheckState(index) == CheckState.Checked)
{
host_list[sitem].connected = false;
}
}
catch(Exception ex)
{
output_textbox.AppendText("connection failed..............." +ex.ToString() +Environment.NewLine);
}
}
You can check/uncheck items in your checkedListBox with this code 您可以使用以下代码检查/取消选中checkedListBox中的项目
checkedListBox.SetItemChecked(item, true);
for more informations go to microsoft documentation 有关更多信息,请转到Microsoft文档。
private void host_listbox_ItemCheck(object sender, ItemCheckEventArgs e)
{
int index = e.Index;
try
{
string sitem = host_listbox.Items[index].ToString();
if (host_list[sitem].is_busy // or whatever indicates that background worker is running or any condition that specifies, that you do not want to let this item to be changed)
e.NewValue = e.CurrentValue; //Change the value back
else
{
//Let the checked state of the item change
This code prevent checked state change if associated background work is running: 如果相关的后台工作正在运行,则此代码可防止检查状态更改:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
workerList = new List<BackgroundWorker>();
for (int i = 0; i < 10; i++)
{
var el = new BackgroundWorker();
el.DoWork += (s, e) =>
{
Thread.Sleep(5000);
};
workerList.Add(el);
checkedListBox1.Items.Add("el " + i);
}
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
var worker = workerList[e.Index];
if (worker.IsBusy)
{
e.NewValue = e.CurrentValue;
return;
}
if (e.NewValue == CheckState.Checked)
worker.RunWorkerAsync();
}
public List<BackgroundWorker> workerList { get; set; }
}
想想,只有funcion解决方案设置了选择模式
CheckedListBox.SelectionMode = SelectionMode.None;
private void ItemCheck(object sender, ItemCheckEventArgs e)
{
if (busy)
e.NewValue = e.CurrentValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.