到目前为止我从未处理过checkedListBox1。我想要制作的程序将受益于使用它,而不是必须使用大量复选框。
我有代码:
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selected = checkedListBox1.SelectedIndex;
this.Text = checkedListBox1.Items[selected].ToString();
}
问题在于,每次我单击该框并使其突出显示时,它都会选择突出显示的对象。我正在寻找的是识别已选择内容的更改,而不是突出显示的内容。
我还想知道的是,如果检查了 CheckListBox 中的第一个索引项以及第三个索引项,我将如何检查它是否为真?
我确信我最终会弄清楚,但看到代码会有很大帮助。
假设我有 3 个盒子:
A 框 = messageBox.Show("a");
B 框 = messageBox.Show("b");
框 C = messageBox.Show("c");
如果选中该框,它将仅显示 mbox。我想知道的是,如何让它检查是否检查了 A 和 C,以便如果我按下按钮,两个消息框将显示“a”,然后显示“c”
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
// a checkbox is changing
// but value is not updated yet
}
private void checkedListBox1_MouseUp(object sender, MouseEventArgs e)
{
Debug.WriteLine(checkedListBox1.CheckedItems.Count);
Debug.WriteLine(checkedListBox1.CheckedItems.Contains(checkedListBox1.Items[0]));
}
我认为你应该在 MouseUp 中检查第一个是否被选中。 _ItemCheck 用于当复选框正在更改但值尚未更新时。
参见参考:http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.items.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.items.aspx
// First show the index and check state of all selected items.
foreach(int indexChecked in checkedListBox1.CheckedIndices) {
// The indexChecked variable contains the index of the item.
MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
}
// Next show the object title and check state for each item selected.
foreach(object itemChecked in checkedListBox1.CheckedItems) {
// Use the IndexOf method to get the index of an item.
MessageBox.Show("Item with title: \"" + itemChecked.ToString() +
"\", is checked. Checked state is: " +
checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)