它不是已选中或未选中的复选框列表视图项 [英] it isn't checked or unchecked item of checkboxed listview

查看:85
本文介绍了它不是已选中或未选中的复选框列表视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的项目有一个checkBoxed listview。

i只想通过编码检查listview的项目:



Hi,
my project has a checkBoxed listview.
i just want to check an item of listview by coding:

listView1.Items[SelectedItemIndex].Checked = true;





它永远不应该检查或通过鼠标点击或其他方式取消选中。

我该怎么办?请帮帮我。



it should never check or uncheck by mouse clicking or other.
what do i do? please help me.

推荐答案

使用WinForms ListView,你正在使用一种恐龙;它有它的怪癖。为了得到你想要的结果,这个解决方案处理那些怪癖:
Using the WinForms ListView, you are using a kind of dinosaur; it has its quirks. To get the result you want, this solution deals with those quirks:
private CheckState preCheckState;

private bool codeChangesCheckState = false;

private bool dontRecurse = false;

private bool initializing = true;

private void Form1_Load(object sender, System.EventArgs e)
{
    initializing = false;
}

private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    // edit #1: ItemCheck is also called when the app runs
    // this change ignores that initial call
    // original code:  if(! codeChangesCheckState) preCheckState = e.CurrentValue;

    if(! initializing && ! codeChangesCheckState) preCheckState = e.CurrentValue;
}

private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
{
    if (codeChangesCheckState || dontRecurse || initializing) return;

    dontRecurse = true;
    e.Item.Checked = preCheckState == CheckState.Checked;
    dontRecurse = false;
}

这是如何/为什么这样做:



1.'initialize标志设置为'true,并阻止愚蠢的ListView的怪癖没有准确地显示程序员在运行应用程序时在设计时检查过的所有ListView项目。是的,这是ListView中的一个错误。



2.'codeChangesCheckState标志用于区分试图在运行时更改ListView项的CheckState的用户,并且你改变了代码中的值。



3.当检查状态变化撤消时,'dontRecurse标志用于避免失控递归(愚蠢的LIstView) 'ItemChecked事件。



4.当用户在运行时更改CheckState 时,Item的现有CheckState记录在ItemCheck中事件,并在ItemChecked事件中恢复保存的CheckState。



在一个更美好的世界中,所有这些都没有必要:你可以取消CheckChange事件'ItemCheck EventHandler ...就像你可以使用提供Before_和After_ EventHandlers的其他WinForms控件一样:)

Here's how/why this works:

1. the 'initialize flag is set to 'true, and prevents the quirk of the stupid ListView from not accurately displaying ALL the ListView Items which have been checked by the programmer at design-time when the app is run. Yes, that's a bug in the ListView.

2. the 'codeChangesCheckState flag is used to distinguish the user trying to change the CheckState of ListView Items at run-time, and your changing the value in your code.

3. the 'dontRecurse flag is used to avoid runaway recursion (stupid LIstView) when the CheckState change is "undone" in the 'ItemChecked event.

4. when the user changes the CheckState at run-time, the Item's existing CheckState is recorded in the ItemCheck event, and the saved CheckState restored in the ItemChecked event.

In a better world all this would not be necessary: you would be able to cancel the CheckChange Event in the 'ItemCheck EventHandler ... as you can do with other WinForms Controls that provide Before_ and After_ EventHandlers :)


这篇关于它不是已选中或未选中的复选框列表视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆