简体   繁体   中英

Can i get the Visible Column Width out of total column width in ListView with Horizontal scrollbar

I have a ListView with View as Details & one of the column has comboboxes to edit the data for each row.

When I resize my columns I am getting a horizontal scrollbar within my ListView in Form. Suppose if I move the second column such a way that half of the column is visible & half can be visible only when scroll is dragged to right.

Can I get this Visible column width of the ListView in any way??

Rgards

Yes you can programatically step through each column in your listview , and get its width.

You can then do the math to calculate the visible area of the column, by using each columns width, and the listview's width itself.

Here is working code. I left it quite simple, to show the steps. You can optimise it for your usage.

        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)

You can also modify it to spit out just a width, and then use your calcualted visible width whenever you need to (Like on a scroll that happend, mouse click etc)

        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

我建议您使用TableLayoutPanel以获得更多机会。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM