Home > Mobile >  Show a MessageBox when the Help Button is pressed
Show a MessageBox when the Help Button is pressed

Time:03-08

I am trying to show a MessageBox (or something that I can put text into) when the context Help Button in the Form's Tile Bar is pressed.

This is the help button I want pressed:

Help Button

Any help would be great! :)

CodePudding user response:

You need to wire up the HelpRequested or HelpButtonClicked events

Plus you need to get the button shown (off by default on a form) by setting

  • HelpButton = true
  • MinimizeBox = false
  • MaximizeBox = false

CodePudding user response:

A few additions to what has already been posted.

The Help Button is shown in the Caption of a Form.
To activate it, set HelpButton = true, MinimizeBox = false and MaximizeBox = false.

  • The HelpButtonClicked event is raised, for the Form only, when the Help Button is clicked.
  • The HelpRequested event is raised when you click on a Control after (when the Mouse Pointer has the shape of a question mark).

To show Help related to a Control, you can subscribe to the HelpRequested event of each Control that provides Help, or you can use just the Form's HelpRequested event for all Controls.
You need to get the child Control that was selected when the Mouse is clicked.
Here I'm using WindowFromPoint() to get the handle of the Control at the position specified by HelpEventArgs.
This function can get the handle of nested Controls.

A Dictionary<Control, string> is used to store and retrieve the Help text for all Controls that should provide a description of their functionality (or whatever else).

Note that the Help is not generated for ContainerControls (the Form itself, Panel, GroupBox etc.) and WindowFromPoint doesn't get the handle of disabled Controls.

using System.Runtime.InteropServices;

public partial class SomeForm : Form

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    internal static extern IntPtr WindowFromPoint(Point point);

    Dictionary<Control, string> help = new Dictionary<Control, string>();

    public SomeForm() {
        InitializeComponent();
        help.Add(someButton, "Help on someButton");
        help.Add(someTextBox, "someTextBox gets help");
        help.Add(someNestedControl, "someNestedControl needs clarifications");
    }

    private void SomeForm_HelpButtonClicked(object sender, CancelEventArgs e) {
        // The Help Button has been clicked
    }

    private void SomeForm_HelpRequested(object sender, HelpEventArgs e) => ShowHelp(e);

    public void ShowHelp(HelpEventArgs e) {
        e.Handled = true;
        var ctl = FromHandle(WindowFromPoint(e.MousePos));
        if (ctl != null && help.ContainsKey(ctl)) {
            MessageBox.Show(help[ctl]);
        }
        else {
            MessageBox.Show("No help for this Control");
        }
    }
}

CodePudding user response:

Sounds like you're new to C# so I'll give you the most simple rundown I can with examples:

In the InitializeComponent() function you need to add:

HelpRequested  = new System.Windows.Forms.HelpEventHandler(this.form_helpRequested);

This will tell the form to run form_helpRequested once the help button is pressed. Then you can implement this event handler function with:

private void textBox_HelpRequested(object sender, System.Windows.Forms.HelpEventArgs hlpevent)
{
    // Your code here
    // Example of a popup with text:
    MessageBox.Show("This is a help popup");
}

This all needs to be in your Form1.cs (or equivilent) script.

For reference on this event handler, I'd suggest giving this article a read: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.control.helprequested?view=windowsdesktop-6.0

  • Related