Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 525751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:42:17+00:00 2026-05-13T08:42:17+00:00

I devised the following code for displaying a control’s Tag property on mouseover. The

  • 0

I devised the following code for displaying a control’s Tag property on mouseover. The code works fine for standard controls such as Labels and TextBoxes but I cannot get it to work for my MenuItems (more specifically ToolStripMenuItems). Could y’all please take a look at my code and tell me what I did wrong? Thanks in advance!

public void Form1_Load(object sender, EventArgs e)

{

...

this.addEventsToAllComponents(this);

}

    private void addEventsToAllComponents(Component component)
{
  if (component is MenuItem)
  {
    MenuItem menuItem = component as MenuItem;
    menuItem.Select += new EventHandler(menuItem_Select);
  }
  else if (component is Control)
  {
    Control ctrl = component as Control;
    foreach (Control control in ctrl.Controls)
    {
      control.MouseEnter += new EventHandler(this.control_MouseEnter);
      control.MouseLeave += new EventHandler(this.control_MouseLeave);
      if (control.HasChildren)
        addEventsToAllComponents(control);
    }
  }
}

    private void menuItem_Select(object sender, EventArgs e)
{
  MenuItem menuItem = sender as MenuItem;

  if (menuItem.Tag.ToString().Length > 0)
    this.toolStripStatusLabel1.Text = menuItem.Tag.ToString();
}

private void control_MouseEnter(object sender, EventArgs e)
{
  Control control = sender as Control;

  if (control.Tag.ToString().Length > 0)
    this.toolStripStatusLabel1.Text = control.Tag.ToString();
}

private void control_MouseLeave(object sender, EventArgs e)
{
  if (this.toolStripStatusLabel1.Text.ToString().Length > 0)
    this.toolStripStatusLabel1.Text = "";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:42:17+00:00Added an answer on May 13, 2026 at 8:42 am

    There are a few problems with your code.

    1st. The Items of a MenuStrip are not children of the Item, so HasChildren will return false. Instead, they are in the Items collection of the MenuStrip. You need to handle a MenuStrip occurrence specially.
    Add the following code in your AddEvents… method below:

    (snip...)
    // old code                    
    if (control.HasChildren)
        AddEventsToAllControls(control);
    //add new code below
    if (control is MenuStrip) {
        MenuStrip ms = control as MenuStrip;
        AddEventsToAllToolStripMenuitems(ms.Items);
    }
    

    And add the new method as follows:

    private void AddEventsToAllToolStripMenuitems (ToolStripItemCollection items) {
        foreach (ToolStripItem tsi in items) {
            tsi.MouseEnter += new EventHandler(this.control_MouseEnter);
            tsi.MouseLeave += new EventHandler(this.control_MouseLeave);
            if (tsi is ToolStripMenuItem) {
                ToolStripMenuItem mi = tsi as ToolStripMenuItem;
                AddEventsToAllToolStripMenuitems(mi.DropDownItems);
            }
        }
    }
    

    2nd. ToolStripItem doesn’t derive from Control, so in MouseEnter the sender as Control statement will fail (control will be null). Do something like this:

    Control control = sender as Control;
    if (control != null && control.Tag != null && control.Tag.ToString().Length > 0)
        this.toolStripStatusLabel1.Text = control.Tag.ToString();
    
    ToolStripItem tsi = sender as ToolStripItem;
    if (tsi != null && tsi.Tag != null && tsi.Tag.ToString().Length > 0)
        this.toolStripStatusLabel1.Text = tsi.Tag.ToString();
    

    (I also added some null checks)

    This should get you going.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sencha's Ext JS (http://www.sencha.com/products/js/) is an excellent framework on the… May 16, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer Didn't find the cause of the problem, but compiling ImageMagick… May 16, 2026 at 7:03 am
  • Editorial Team
    Editorial Team added an answer MSBuild has two separate metadata properties called %(Filename) (which is… May 16, 2026 at 7:03 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.