首先在选中列表框中显示checkitems [英] show checkeditems first in checked listbox

查看:129
本文介绍了首先在选中列表框中显示checkitems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts,



如何在Checked列表框中首先显示选中的项目并在下面取消选中?基本上类似于基于已检查和未检查值的排序。

请帮助我



非常感谢您的帮助。

Hello Experts,

How to I show checked items in Checked listbox first and unchecked below? Basically something like sort based on checked and unchecked value.
Kindly help me

Your help is much appreciated.

推荐答案

我把你的问题作为一个小挑战,提出了以下内容。



我创建了一个自定义子类,继承自 CheckedListBox 以覆盖 Sort() -method。



我将 CheckedItems -collection中的项目复制到列表< object> 中并取消选中将物品带入另一个列表< object> ,取出所有物品并排除已检查物品。



然后通过在每个列表上调用 Sort()来完成在已检查和未检查的区域内的排序;按字母顺序排列。此方法有一些重载,允许您指定自定义Comparer。如果你想以不同的方式排序,你必须在那里扩展代码。



在对列表进行排序后,两者都被迭代并且 Items -Collection被现在排序的项覆盖,同时使用true / false调用 SetItemChecked(..)(因为 CheckedListBox 有两个独立的集合来存储项目及其检查状态,这些集合不会自动关联。

因为 SetItemChecked(。 。)调用 OnItemCheck(..)以提高 ItemCheck -event,我使用布尔标志 SortingInProgress 来排序时禁止该事件。



因为排序()是一个受保护的方法,我还实现了一个 SortNow() -method,以便能够随时对项目进行排序。



VB.NET

I took your question as a small challenge and came up with the following.

I created a custom subclass, inheriting from CheckedListBox in order to override the Sort()-method.

I copied the items in the CheckedItems-collection into a List<object> and the un-checked items into another List<object> by taking all items and excluding the checked items.

The "sub-sorting" within the checked- and unchecked regions is then done by calling Sort() on each of those Lists; alphabetical by default. This method has some overloads that allow you to specify a custom Comparer. If you want to sort differently, you would have to extend the code there.

After sorting the Lists, both are iterated and the indices of the Items-Collection are overwritten with the now sorted items, while calling SetItemChecked(..) with true/false (because the CheckedListBox has two separate collections to store the items and their checked-state and these aren't associated automatically).
Because SetItemChecked(..) calls OnItemCheck(..) in order to raise the ItemCheck-event, I used a boolean flag SortingInProgress to suppress that event while sorting.

Because Sort() is a protected method, I also implemented a SortNow()-method to be able to sort the items whenever you like.

VB.NET :
Public Class SortByCheckedListBox
    Inherits CheckedListBox

    Private SortingInProgress As Boolean

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub SortNow()
        Sort()
    End Sub

    Protected Overrides Sub OnItemCheck(ice As ItemCheckEventArgs)
        If Not SortingInProgress Then
            MyBase.OnItemCheck(ice)
        End If
    End Sub

    Protected Overrides Sub Sort()
        If Items.Count > 1 Then
            SortingInProgress = True

            Dim checkedItems1 As New List(Of Object)(CheckedItems.OfType(Of Object)())
            Dim uncheckedItems As New List(Of Object)(Items.OfType(Of Object)().Except(checkedItems1))

            checkedItems1.Sort()
            uncheckedItems.Sort()

            Dim item As Integer = 0

            For checkd As Integer = 0 To checkedItems1.Count - 1
                Items(item) = checkedItems1(checkd)
                SetItemChecked(item, True)
                item += 1
            Next

            For uncheckd As Integer = 0 To uncheckedItems.Count - 1
                Items(item) = uncheckedItems(uncheckd)
                SetItemChecked(item, False)
                item += 1
            Next

            SortingInProgress = False
        End If
    End Sub
End Class





C#



C# :

public class SortByCheckedListBox : CheckedListBox
{
    private bool SortingInProgress;

    public SortByCheckedListBox()
        : base()
    { }

    public void SortNow()
    {
        Sort();
    }

    protected override void OnItemCheck(ItemCheckEventArgs ice)
    {
        if (!SortingInProgress)
            base.OnItemCheck(ice);
    }

    protected override void Sort()
    {
        if (Items.Count > 1)
        {
            SortingInProgress = true;

            List<object> checkedItems = new List<object>(CheckedItems.OfType<object>());
            List<object> uncheckedItems = new List<object>(Items.OfType<object>().Except(checkedItems));

            checkedItems.Sort();
            uncheckedItems.Sort();

            int item = 0;

            for (int checkd = 0; checkd < checkedItems.Count; checkd++, item++)
            {
                Items[item] = checkedItems[checkd];
                SetItemChecked(item, true);
            }

            for (int uncheckd = 0; uncheckd < uncheckedItems.Count; uncheckd++, item++)
            {
                Items[item] = uncheckedItems[uncheckd];
                SetItemChecked(item, false);
            }

            SortingInProgress = false;
        }
    }
}


这篇关于首先在选中列表框中显示checkitems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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