Display Tooltip/Hint Text of Menu Items in Status Bar C#

Posted in: Programming @en |

This post is also available in: Indonesian

tooltip-in-statusbar

This post is also available in: Indonesian

Have you ever use Windows Explorer or other programs, then you’ll probably notice the hint text in the status bar whenever you hover the mouse pointer on a menu item. This will help user and add application’s usability by providing user support. In C# after we set tooltip/hint text in each menu items, it will not immediately appear in the Status Bar (in .NET it is commonly known with StatusStrip). In Delphi it’s easy to display the hint text in the status bar by assigning handler in Application.OnHint event as follows:

procedure TForm1.ShowHint(Sender: TObject);
begin
  StatusBar1.SimpleText:=Application.Hint;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnHint:=ShowHint;
end;

Since I’m also relatively a newbie in C# WinForms and doesn’t know the efficient and easier way to display hint text in the Status Strip, the steps that I use are traversing/enumerating each menu items and assign each with event handler on event MouseEnter and MouseLeave if it has a tooltip text.

//constructor Form1
public Form1()
{
    InitializeComponent();
    InitializeMenuHint();
}

private void InitializeMenuHint()
{
    foreach (ToolStripMenuItem item in menuStrip1.Items)
    {
        TraverseMenuItemHint(item);
    }
}
private void MenuHint_Enter(object sender, EventArgs e)
{
    statusStrip1.Text = (sender as ToolStripMenuItem).ToolTipText;
    //bila membuat Status Label di Status Strip gunakan di bawah ini
    //toolStripStatusLabel1.Text = (sender as ToolStripMenuItem).ToolTipText;
}

private void MenuHint_Leave(object sender, EventArgs e)
{
    statusStrip1.Text = "";
    //bila membuat Status Label di Status Strip gunakan di bawah ini
    //toolStripStatusLabel1.Text = "";
}

private void TraverseMenuItemHint(ToolStripMenuItem element)
{
    for (int i = 0; i < element.DropDownItems.Count; i++)
    {
        if (!(element.DropDownItems[i] is ToolStripSeparator))
        {
            ToolStripMenuItem item = element.DropDownItems[i] as ToolStripMenuItem;
            if (item.ToolTipText != "")
            {
                item.MouseEnter += MenuHint_Enter;
                item.MouseLeave += MenuHint_Leave;
            }
            TraverseMenuItemHint(item);

        }
    }
}

UPDATE:
The solution above only works to display the tooltip when the mouse is hovering menu items, when using the keyboard it will not work. Therefore, I make another, a simpler way in order to display the tooltip text in the status bar.

The steps are:

  1. Create internal class derived from ToolStripMenuItem in Form
    using ToolStripMenuItem
  2. Add event Hint that will be called when the menu item is selected
  3. Create an event handler Menu_Hint in the Form
  4. Traverse/Enumerate ToolStripMenuItem, when a tooltip text is not empty string, assign event handler Menu_Hint to Hint event
  5. Change all classes System.Windows.Forms.ToolStripMenuItem in Form
    .designer.cs
    into ToolStripmenuItem (from the internal class we create before).

Complete source code like the following

public partial class FormMain : System.Windows.Forms.Form
{
    internal class ToolStripMenuItem : System.Windows.Forms.ToolStripMenuItem
    {
        public event EventHandler Hint;
        protected virtual void OnHint(EventArgs e)
        {
            if (Hint != null)
                Hint(this, e);
        }
        public override bool Selected
        {
            get
            {
                if (base.Selected == true)
                {
                    OnHint(new EventArgs());
                }
                return base.Selected;
            }
        }
    }
    public FormMain()
    {
        InitializeComponent();
        InitializeMenuHint();
    }
    private void InitializeMenuHint()
    {

        foreach (ToolStripMenuItem item in MainMenu.Items)
        {
            TraverseMenuItemHint(item);
        }
    }

    private void MenuHint_Hint(object sender, EventArgs e)
    {
        statusStrip1.Text = (sender as ToolStripMenuItem).ToolTipText;
        //bila membuat Status Label di Status Strip gunakan di bawah ini
        //toolStripStatusLabel1.Text = (sender as ToolStripMenuItem).ToolTipText;
    }

    private void TraverseMenuItemHint(ToolStripMenuItem element)
    {
        for (int i = 0; i < element.DropDownItems.Count; i++)
        {
            if (!(element.DropDownItems[i] is ToolStripSeparator))
            {
                ToolStripMenuItem item = element.DropDownItems[i] as ToolStripMenuItem;
                if (item.ToolTipText != "")
                {
                    item.Hint += MenuHint_Hint;
                }
                TraverseMenuItemHint(item);

            }
        }
    }
}



Speak Up!

Leave your own comment

Notify me of follow-up comments via e-mail (or subscribe here).




 

Share

Subscribe Feed

Email

Facebook

Twitter

Delicious

Digg

StumbleUpon

Google Buzz

Deviantart