问题描述
我正在使用列出了几个复选框项目的列表框。
I'm using a listbox with several check boxed items listed.
当我点击列表框时,我需要确定是否已选中所选项目。
When I click the listbox I need to determine if the selected item is checked already.
如果是,则取消选中它 - 如果不是,则检查它。
If it is then uncheck it - if it isn't then check it.
我的代码.. 。
              bool bool_adders = lstbx_TMDAdders.GetItemChecked(0); //项目索引0是否已检查?
             开关(bool_adders)
              {
                  case true://已经检查过方框
                      lstbx_TMDAdders.SetItemChecked(0,false); //这应该取消选中该框,但不是。
                     休息;
                  case false://盒子尚未检查
                      lstbx_TMDAdders.SetItemChecked(0,true); //这将勾选方框
                     休息;
              }
bool bool_adders = lstbx_TMDAdders.GetItemChecked(0); // is item index 0 checked?
switch (bool_adders)
{
case true: //box is already checked
lstbx_TMDAdders.SetItemChecked(0, false); // this should uncheck the box but doesn't.
break;
case false: //box is not already checked
lstbx_TMDAdders.SetItemChecked(0, true); //this will check the box
break;
}
我做错了什么?显然是因为它不起作用。
Am I doing something wrong? obviously so because it isn't working.
使用VS 2015 C#
Using VS 2015 C#
请告诉我。
tac
推荐答案
听起来您正在使用CheckedListBox而不是ListBox。您可以订阅以下事件ItemCheck。
Sounds like you are working with a CheckedListBox not a ListBox. You can subscribe to the following event ItemCheck.
checkedListBox1.ItemCheck += CheckedListBox1_ItemCheck;
那么
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.CurrentValue == CheckState.Checked)
{
// do something
}
else if (e.CurrentValue == CheckState.Unchecked)
{
// do something else
}
}
查看所有内容(包括当前值)
To see everything (including what the current value is going to be)
private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.CurrentValue == CheckState.Checked)
{
// do something
}
else if (e.CurrentValue == CheckState.Unchecked)
{
// do something else
}
Console.WriteLine(
这篇关于已选中列表框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!