Results 1 to 3 of 3

Thread: trapping a right click of a column header

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    2

    trapping a right click of a column header

    Here's the 911:
    I need to trap the right click of a columnheader in a control that inherits from Listview. I realize this means overriding the WndProc method and trapping the WM_Notify, which in turn needs to be converted to a NMHDR structure. However, i am having problems with this not returning the right code. Does anyone have any idea how to do it or can at least point me in the right direction. I am currently using Visual Studio 2003 running on Windows XP pro.

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    i answered your ? on dotnetforums.net , but here ya go.
    i spent an hour or so putting this together...
    VB Code:
    1. Imports System.Runtime.InteropServices
    2. '/// top of form's code window^^^
    3.  
    4.     Private WithEvents listviewSubclass As SubclassWindow
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         listviewSubclass = New SubclassWindow()
    8.         listviewSubclass.IsSubClassed = True
    9.         listviewSubclass.AddControl(ListView1)
    10.     End Sub
    11.  
    12.     Private Sub listviewSubclass_MyBaseProc(ByVal m As Message) Handles listviewSubclass.MyBaseProc
    13.         If m.Msg = _Win32.WM_NOTIFY Then
    14.             '/// now we create / fill our structure using Marshal.PtrToStructure...
    15.             Dim nh As NMHDR = Marshal.PtrToStructure(m.LParam, GetType(NMHDR))
    16.             If nh.code = _Win32.NM_RCLICK Then
    17.                 MessageBox.Show("Right button Clicked ColumnHeader")
    18.             End If
    19.         End If
    20.  
    21.     End Sub
    22.  
    23. '/// after your End Class of form...
    24.  
    25. Public Class SubclassWindow
    26.     Inherits System.Windows.Forms.NativeWindow
    27.     Public Event MyBaseProc(ByVal m As Message)
    28.  
    29.     Public IsSubClassed As Boolean = False
    30.  
    31.     Public Sub AddControl(ByVal ctrl As Control)
    32.         MyBase.AssignHandle(ctrl.Handle)
    33.     End Sub
    34.  
    35.     Protected Overrides Sub WndProc(ByRef m As Message)
    36.         If IsSubClassed Then
    37.             RaiseEvent MyBaseProc(m)
    38.         End If
    39.  
    40.         MyBase.WndProc(m)
    41.     End Sub
    42.  
    43.     Public Overrides Sub ReleaseHandle()
    44.         MyBase.ReleaseHandle()
    45.     End Sub
    46.  
    47. End Class
    48. Public Enum _Win32 As Integer
    49.     WM_NOTIFY = &H4E
    50.     NM_RCLICK = -5
    51. End Enum
    52.  
    53. <StructLayout(LayoutKind.Sequential)> _
    54. Public Structure NMHDR
    55.     Public hwndFrom As Integer
    56.     Public idfrom As Integer
    57.     Public code As Integer
    58. End Structure
    hope it helps
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    911? Don't you mean 411? Or do you want the police or emergency services to answer your post?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width