How to programmatically activate Context Help Mode on a Windows Form in C#

465 Views Asked by At

I am building a Windows Forms application in C# (Visual Studio, .net framework 4.7.2).

I have created a form, and set the HelpButton property to True, so that the Context Help button appears on the window's title bar as shown here:

enter image description here

Under normal circumstances, the user can click this help button, which activates Context Help Mode. Then they click on a control to display context-sensitive help. (The HelpRequested event is dispatched to the control.)

I want to be able to activate the Context Help Mode using a keyboard shortcut so that the user doesn't have to CLICK the help button on the title bar. In other words, I need to programmatically activate Context Help Mode. How is this done?

Please understand that I am NOT asking how to open context-sensitive help by pressing a hotkey on a control. (I.e. press F1 while a control has focus to bring up help about that control). This is NOT what I want to do. I want to programmatically activate the Context Help Mode (where the cursor turns to a question mark) so that the user can then click on the desired control to get context-sensitive help.)

Forms have the "OnHelpButtonClicked()" function, which is supposed to fire the Help Button clicked event, so I tried the following with no success:

CancelEventArgs ee = new CancelEventArgs();
this.OnHelpButtonClicked(ee);

How can the Context Help Mode be turned on programmatically without clicking the Help button on the window title bar?

Thank you for any help!

1

There are 1 best solutions below

0
On

The solution, thanks to @nilsK and the following post: How to do context help ("what is this?" button) in WinForms? requires sending a Windows Message to the form to trigger the Context Help Mode:

In the window's main class:

private const int SC_CONTEXTHELP = 0xf180;
private const int WM_SYSCOMMAND = 0x112;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);

In the keypress handler or other event to trigger Context Help Mode:

SendMessage(this.Handle, WM_SYSCOMMAND, (IntPtr)SC_CONTEXTHELP, IntPtr.Zero);

Works!