How can I catch the autostart double-click event in a list in VB.NET?

I am using Visual Studio 2008 and VB.NET. I have a listview control in my form and I added columns using the window designer. As you know, if you double-click the sizer or divider element or whatever you name between the two columns, the column on the left will be authorized (unless you disable it). How can I catch this particular event? The event ColumnWidthChangedand the event DoubleClickare likely candidates, but ColumnWidthChangedI do not see in the event to determine if it has been authorized. In the same way, there is no easy way to catch what was pressed precisely with the event DoubleClick. Does anyone have any ideas how I can catch this type of specific event?

+3
source share
1 answer

Detecting events in the listview header is pretty tricky.

You will need to create your own header to replace the one that it usually uses, and then listen to the corresponding messages. As far as I know, there are no specific columns for specific column descriptors.

The next class subclasses ListView and adds a handler that detects double-clicking between columns. I think it is as close as it seems.

I hope this helps you a bit.

    Class MyListView
        Inherits ListView

        Protected Overrides Sub CreateHandle()
            MyBase.CreateHandle()
            New HeaderControl(Me)
        End Sub

        Private Class HeaderControl
            Inherits NativeWindow
            Private _parent As ListView = Nothing

            <DllImport("User32.dll", CharSet := CharSet.Auto, SetLastError := True)> _
            Public Shared Function SendMessage(hWnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr) As IntPtr
            End Function

            Public Sub New(parent As ListView)
                _parent = parent

                Dim header As IntPtr = SendMessage(parent.Handle, (&H1000 + 31), IntPtr.Zero, IntPtr.Zero)
                Me.AssignHandle(header)
            End Sub


            Protected Overrides Sub WndProc(ByRef message As Message)
                Const  WM_LBUTTONDBLCLK As Integer = &H203

                Select Case message.Msg
                    Case WM_LBUTTONDBLCLK
                        Dim position As Point = Control.MousePosition
                        Dim relative As Point = _parent.PointToClient(position)

                        Dim rightBorder As Integer = 0
                        For Each c As ColumnHeader In _parent.Columns
                            rightBorder += c.Width
                            If relative.X > (rightBorder - 6) AndAlso relative.X < (rightBorder + 6) Then
                                MessageBox.Show([String].Format("Double-click after column '{0}'", c.Text))
                            End If
                        Next
                        Exit Select
                End Select


                MyBase.WndProc(message)


            End Sub

        End Class
    End Class

You will need to enable the use of System.Runtime.InteropServices; expression for this.

0
source

All Articles