How do I cancel a context menu programatically?

Platform: WinForms| Category: Menus

First keep track of which control is showing the ContextMenu by listening to the menu’s Popup event and querying for the SourceControl.

Then when you are ready to cancel the popup, do as follows:


[C#]
		[DllImport('user32.dll', CharSet=CharSet.Auto)]
		extern internal static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

		private void Timer_Tick(object sender, EventArgs e)
		{
			if(menuSourceControl != null)
				SendMessage(menuSourceControl.Handle, 0x001F/*WM_CANCELMODE*/, IntPtr.Zero, IntPtr.Zero);

		}

[VB.Net]
		 _ 
		extern internal static IntPtr SendMessage(IntPtr hWnd, Integer msg, IntPtr wParam, IntPtr lParam)
 
		Private  Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
			If Not menuSourceControl Is Nothing Then
				SendMessage(menuSourceControl.Handle, 0x001F, IntPtr.Zero, IntPtr.Zero)
			End If
 
		End Sub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.