我可以使用水平滚动条在ListView中获取总列宽的可见列宽度

时间:2015-12-01 09:48:05

标签: c# .net winforms

我有一个ListView with View as Details&其中一列有组合框来编辑每一行的数据。

当我调整列的大小时,我在FormView中的ListView中获得了一个水平滚动条。假设我移动第二列,使得一半的列可见并且只有当滚动向右拖动时,才能看到一半。

我可以以任何方式获得ListView的可见列宽吗?

Rgards

2 个答案:

答案 0 :(得分:0)

是的,您可以以编程方式逐步浏览listview中的每一列,并获取其宽度。

然后,您可以使用每个列宽和列表视图的宽度本身来计算列的可见区域。

这是工作代码。我离开它非常简单,以显示步骤。您可以根据自己的需要进行优化。

        Dim totalWdith As Integer = SOlistview.Width
        Dim combinedColumnWidth As Integer = 0
        Dim visibleSpaceLeft As Integer = 0

        Dim columnCount As Integer = SOlistview.Columns.Count
        Dim weHaveClipping As Boolean = False

        For i = 0 To columnCount - 1

            If combinedColumnWidth + SOlistview.Columns(i).Width > totalWdith Then

                '//we will have clipping occur if we add this column...

                visibleSpaceLeft = SOlistview.Width - combinedColumnWidth
                weHaveClipping = True
                Exit For

            Else

                '//no clipping yet, so add this column to my math

                combinedColumnWidth += SOlistview.Columns(i).Width

            End If

        Next

        Dim myDropDown As New ComboBox

        If weHaveClipping Then
            myDropDown.Top = e.Y - myDropDown.Height
            myDropDown.Left = combinedColumnWidth
            myDropDown.Width = visibleSpaceLeft
        Else
            '//run your normal code to place the combobox....
        End If


        SOlistview.Controls.Add(myDropDown)

您也可以修改它以仅吐出一个宽度,然后在需要时使用您计算出的可见宽度(如在滚动时发生,点击鼠标等)

        private function _get_clippedColumn_visible_width() as int16

        Dim totalWdith As Integer = SOlistview.Width
        Dim combinedColumnWidth As Integer = 0
        Dim visibleSpaceLeft As Integer = 0

        Dim columnCount As Integer = SOlistview.Columns.Count
        Dim weHaveClipping As Boolean = False

        For i = 0 To columnCount - 1

            If combinedColumnWidth + SOlistview.Columns(i).Width > totalWdith Then

                '//we will have clipping occur if we add this column...

                visibleSpaceLeft = SOlistview.Width - combinedColumnWidth
                weHaveClipping = True
                Exit For

            Else

                '//no clipping yet, so add this column to my math

                combinedColumnWidth += SOlistview.Columns(i).Width

            End If

         Next

         return visibleSpaceLeft 

        End Function

答案 1 :(得分:-1)

我建议你使用TableLayoutPanel来获得更多机会。