none
如何将CheckBoxList分成多列? RRS feed

  • 问题

  • 如上图,我希望在VB.Net中,用CheckedListBox控件实现,并且可以通过点击不同的列标题,如“文件名”“大小”等,进行顺序或者逆序的排列。

    以前曾经实现过,不过现在这段代码找不到了。


    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2014年12月9日 4:57

答案

  • 如上图,我希望在VB.Net中,用CheckedListBox控件实现,并且可以通过点击不同的列标题,如“文件名”“大小”等,进行顺序或者逆序的排列。

    以前曾经实现过,不过现在这段代码找不到了。


    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    从图片来看, 这个控件需要显示每一列的列名, 单从这一点来看, 它并不是由CheckedListBox控件来实现的, 因为我们不能给它设置column,甚至每一列头显示的文本。

    有关CheckedListBox显示多列, 其实是根据当前控件的高度来判断是否有足够空间来显示所有的选项,当高度太小并且设置了 MultiColumn为True时,控件会根据设置的ColumnWidth来多列显示。

    要实现图片的效果,我建议你用DataGridView添加一个CheckBox的column来实现, 或者用ListView设置其 CheckBoxes属性为True来实现。

    因为你需要对每一列实现排序,而DataGridView已经实现了简单的排序功能,我这里列举一个ListView排序的例子。

    1. 设置CheckBoxes属性为True, View属性为 Details. 添加列头和对应行的数据。

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load    
            ListView1.CheckBoxes = True
            ListView1.View = View.Details
            For i = 1 To 11
                ListView1.Columns.Add("Column" & i)
            Next
            For index = 1 To 10
                Dim lvi = New ListViewItem(index.ToString())
                ListView1.Items.Add(lvi)
                For i = 1 To 10
                    lvi.SubItems.Add(index.ToString() & "-" & i)
                Next
            Next
        End Sub

    2.自定义一个排序的类

    Public Class ListViewItemComparer
        Implements IComparer
        Private col As Integer
        Private order As SortOrder
    
        Public Sub New()
            col = 0
            order = SortOrder.Ascending
        End Sub
    
        Public Sub New(column As Integer, order As SortOrder)
            col = column
            Me.order = order
        End Sub
    
        Public Function Compare(x As Object, y As Object) As Integer _
                            Implements System.Collections.IComparer.Compare
            Dim returnVal As Integer = -1
            returnVal = [String].Compare(CType(x,  _
                            ListViewItem).SubItems(col).Text, _
                            CType(y, ListViewItem).SubItems(col).Text)
            ' Determine whether the sort order is descending.
            If order = SortOrder.Descending Then
                ' Invert the value returned by String.Compare.
                returnVal *= -1
            End If
            Return returnVal
        End Function
    End Class


    3. 在列头的单击事件中排序。

        Dim sortColumn As Integer = -1
        Private Sub ListView1_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListView1.ColumnClick
            If e.Column <> sortColumn Then
                ' Set the sort column to the new column.
                sortColumn = e.Column
                ' Set the sort order to ascending by default.
                ListView1.Sorting = SortOrder.Ascending
            Else
                ' Determine what the last sort order was and change it.
                If ListView1.Sorting = SortOrder.Ascending Then
                    ListView1.Sorting = SortOrder.Descending
                Else
                    ListView1.Sorting = SortOrder.Ascending
                End If
            End If
            ' Call the sort method to manually sort.
            ListView1.Sort()
            ' Set the ListViewItemSorter property to a new ListViewItemComparer
            ' object.
            ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column, ListView1.Sorting)
    
        End Sub

    有关自定义排序,你可以参看以下文档

    #使用 Windows Form 按列排序 ListView 项目

    http://msdn.microsoft.com/zh-cn/library/ms996467.aspx

    如果不需要自定义排序的话,可以简单地在单击事件里调用Sort()方法。

     Dim sortColumn As Integer = -1
        Private Sub ListView1_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListView1.ColumnClick
            If e.Column <> sortColumn Then
                ' Set the sort column to the new column.
                sortColumn = e.Column
                ' Set the sort order to ascending by default.
                ListView1.Sorting = SortOrder.Ascending
            Else
                ' Determine what the last sort order was and change it.
                If ListView1.Sorting = SortOrder.Ascending Then
                    ListView1.Sorting = SortOrder.Descending
                Else
                    ListView1.Sorting = SortOrder.Ascending
                End If
            End If
            ' Call the sort method to manually sort.
            ListView1.Sort()
        End Sub



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年12月10日 2:56
    版主

全部回复

  • 如上图,我希望在VB.Net中,用CheckedListBox控件实现,并且可以通过点击不同的列标题,如“文件名”“大小”等,进行顺序或者逆序的排列。

    以前曾经实现过,不过现在这段代码找不到了。


    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    从图片来看, 这个控件需要显示每一列的列名, 单从这一点来看, 它并不是由CheckedListBox控件来实现的, 因为我们不能给它设置column,甚至每一列头显示的文本。

    有关CheckedListBox显示多列, 其实是根据当前控件的高度来判断是否有足够空间来显示所有的选项,当高度太小并且设置了 MultiColumn为True时,控件会根据设置的ColumnWidth来多列显示。

    要实现图片的效果,我建议你用DataGridView添加一个CheckBox的column来实现, 或者用ListView设置其 CheckBoxes属性为True来实现。

    因为你需要对每一列实现排序,而DataGridView已经实现了简单的排序功能,我这里列举一个ListView排序的例子。

    1. 设置CheckBoxes属性为True, View属性为 Details. 添加列头和对应行的数据。

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load    
            ListView1.CheckBoxes = True
            ListView1.View = View.Details
            For i = 1 To 11
                ListView1.Columns.Add("Column" & i)
            Next
            For index = 1 To 10
                Dim lvi = New ListViewItem(index.ToString())
                ListView1.Items.Add(lvi)
                For i = 1 To 10
                    lvi.SubItems.Add(index.ToString() & "-" & i)
                Next
            Next
        End Sub

    2.自定义一个排序的类

    Public Class ListViewItemComparer
        Implements IComparer
        Private col As Integer
        Private order As SortOrder
    
        Public Sub New()
            col = 0
            order = SortOrder.Ascending
        End Sub
    
        Public Sub New(column As Integer, order As SortOrder)
            col = column
            Me.order = order
        End Sub
    
        Public Function Compare(x As Object, y As Object) As Integer _
                            Implements System.Collections.IComparer.Compare
            Dim returnVal As Integer = -1
            returnVal = [String].Compare(CType(x,  _
                            ListViewItem).SubItems(col).Text, _
                            CType(y, ListViewItem).SubItems(col).Text)
            ' Determine whether the sort order is descending.
            If order = SortOrder.Descending Then
                ' Invert the value returned by String.Compare.
                returnVal *= -1
            End If
            Return returnVal
        End Function
    End Class


    3. 在列头的单击事件中排序。

        Dim sortColumn As Integer = -1
        Private Sub ListView1_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListView1.ColumnClick
            If e.Column <> sortColumn Then
                ' Set the sort column to the new column.
                sortColumn = e.Column
                ' Set the sort order to ascending by default.
                ListView1.Sorting = SortOrder.Ascending
            Else
                ' Determine what the last sort order was and change it.
                If ListView1.Sorting = SortOrder.Ascending Then
                    ListView1.Sorting = SortOrder.Descending
                Else
                    ListView1.Sorting = SortOrder.Ascending
                End If
            End If
            ' Call the sort method to manually sort.
            ListView1.Sort()
            ' Set the ListViewItemSorter property to a new ListViewItemComparer
            ' object.
            ListView1.ListViewItemSorter = New ListViewItemComparer(e.Column, ListView1.Sorting)
    
        End Sub

    有关自定义排序,你可以参看以下文档

    #使用 Windows Form 按列排序 ListView 项目

    http://msdn.microsoft.com/zh-cn/library/ms996467.aspx

    如果不需要自定义排序的话,可以简单地在单击事件里调用Sort()方法。

     Dim sortColumn As Integer = -1
        Private Sub ListView1_ColumnClick(sender As Object, e As ColumnClickEventArgs) Handles ListView1.ColumnClick
            If e.Column <> sortColumn Then
                ' Set the sort column to the new column.
                sortColumn = e.Column
                ' Set the sort order to ascending by default.
                ListView1.Sorting = SortOrder.Ascending
            Else
                ' Determine what the last sort order was and change it.
                If ListView1.Sorting = SortOrder.Ascending Then
                    ListView1.Sorting = SortOrder.Descending
                Else
                    ListView1.Sorting = SortOrder.Ascending
                End If
            End If
            ' Call the sort method to manually sort.
            ListView1.Sort()
        End Sub



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年12月10日 2:56
    版主
  • 非常感谢版主,虽有不解,但果断标记为答案以及有帮助,并强烈呼吁加精。

    什么?个性签名?哼!打死我也不签。既然你们都不觉得我聪明,那就叫我BenQ吧。好歹也是国际大品牌。

    2014年12月11日 4:35