如何取消选中checkedListBox1并正确编写字符串? [英] How to uncheck checkedListBox1 and write the string properly?
问题描述
我有checkedListBox1控件。我想写成一个字符串checked3487,只有选中的复选框文本。 (复选框附近的文字)。
当我按下一个复选框时,检查状态直到ItemCheck事件发生后才更新,但是if(e.NewValue == CheckState.Checked) )正在实时更新检查状态,但仅针对Checked状态。
当我UNCHECK时,它会在2次点击鼠标后更新(在ItemCheck事件发生后)。
如何正确取消选中并写入字符串?
我在google上搜索过,我做了10种不同的编码变种,我绝望了。我想念的是什么?
I have checkedListBox1 control. I want to write into a string "checked3487", ONLY the checked checkboxes text. (the text near the checkbox).
When I press one checkbox, "The check state is not updated until after the ItemCheck event occurs", but the "if (e.NewValue == CheckState.Checked)" is updating in realtime the check state, but only for the Checked state.
When I UNCHECK, it is updating after 2 click of the mouse, (after the ItemCheck event occurs).
How to uncheck and write the string properly?
Ive searched on google, I done 10 different variants of coding, and im desperate. What I miss?
string checked3487 = ""; List<string> checkedList = new List<string>();
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
listBox1.Items.Clear(); checked3487 = ""; checkedList.Clear();
foreach (var item in checkedListBox1.CheckedItems)
checkedList.Add(item.ToString());
if (e.NewValue == CheckState.Checked)
{
checkedList.Add(checkedListBox1.Items[e.Index].ToString());
}
if (e.NewValue == CheckState.Unchecked)
{
//?????????????????????????????????????????????????????????????????????/
}
foreach (string item in checkedList)
{
checked3487 += item;
}
}
推荐答案
现在正在工作!
这是一个字符串毕竟是操纵。该死的。
我得到了启发,我找到了解决方案。
这里是:
Now is working!
it was a string manipulation after all. Damn it.
I got inspired and I find the solution.
here it is:
if (e.NewValue == CheckState.Unchecked)
{
if (checkedList.Contains(checkedListBox1.Items[e.Index].ToString()))
{
int i = checkedList.IndexOf(checkedListBox1.Items[e.Index].ToString());
checkedList.RemoveAt(i);
}
}
这篇关于如何取消选中checkedListBox1并正确编写字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!