热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

以静态方法获取项目CheckState-GetitemCheckStateinastaticmethod

ImadeanextensionmethodtoswapplacesoftwoitemsinaCheckedListBox.Themethodisputinas

I made an extension method to swap places of two items in a CheckedListBox. The method is put in a static Utilities class. The problem is that the CheckState doesn't travel. So if I move a checked item up in the list, the checkbox state will stay and the moved item will take over the CheckState from the item it's replacing.

我做了一个扩展方法来交换CheckedListBox中两个项目的位置。该方法放在静态Utilities类中。问题是CheckState不会移动。因此,如果我在列表中移动已检查的项目,则复选框状态将保留,移动的项目将从其替换的项目接管CheckState。

My code looks like this:

我的代码如下所示:

public static System.Windows.Forms.CheckedListBox.ObjectCollection Swap(this System.Windows.Forms.CheckedListBox.ObjectCollection lstBoxItems, int indexA, int indexB)
{
    if (indexB > -1 && indexB 

What I want is something like this (which doesn't work obviously)

我想要的是这样的东西(显然不起作用)

public static System.Windows.Forms.CheckedListBox.ObjectCollection Swap(this System.Windows.Forms.CheckedListBox.ObjectCollection lstBoxItems, int indexA, int indexB)
{
    if (indexB > -1 && indexB 

The code is simply called like this

代码就像这样简单地调用

myCheckedListBox.Items.Swap(selectedIndex, targetIndex);

2 个解决方案

#1


3  

I haven't used the CheckedListBox before, but if I had to hazard a guess looking at the MSDN docs for it, I would say you'd want to use the GetItemCheckedState and the SetItemCheckedState methods. However, that also means you'd have to pass in the CheckedListBox as well rather than just its .Items ObjectCollection.

我之前没有使用过CheckedListBox,但如果我不得不冒险猜测它的MSDN文档,我会说你想要使用GetItemCheckedState和SetItemCheckedState方法。但是,这也意味着您必须传入CheckedListBox,而不仅仅是传递.Items ObjectCollection。

public static System.Windows.Forms.CheckedListBox Swap(this System.Windows.Forms.CheckedListBox listBox, int indexA, int indexB)
{
    var lstBoxItems = listBox.Items;
    if (indexB > -1 && indexB 

So naturally your calling code would change to something like this:

所以你的调用代码自然会变成这样的东西:

myCheckedListBox.Swap(selectedIndex, targetIndex);

Also, note that my method returns the input CheckedListBox as well instead of the ObjectCollection; figured that would be more appropriate now given the change of signature parameters.

另外,请注意我的方法也返回输入CheckedListBox而不是ObjectCollection;考虑到签名参数的变化,现在更合适。

#2


1  

Maybe the problem is that you should be first getting the current check state of actual list box item instead of from the copy. You already know that the list box is managing the checks separate from the item list content!

也许问题是你应该首先获得实际列表框项目的当前检查状态而不是副本。您已经知道列表框正在管理与项目列表内容分开的支票!

You also should consider getting the current checked states for both items A and B. After you perform the item swap then reapply the checked state to the two items so you maintain that status for both swapped items.

您还应考虑获取项目A和B的当前已检查状态。执行项目交换后,将已检查状态重新应用于这两个项目,以便为两个交换项目保持该状态。


推荐阅读
author-avatar
手机用户2502863963
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有