在鼠标悬停时设置状态栏文本 - 适用于控件但不适用于 MenuItems

Set Statusbar Text on Mouse Hover - Works for controls but not MenuItems(在鼠标悬停时设置状态栏文本 - 适用于控件但不适用于 MenuItems)

本文介绍了在鼠标悬停时设置状态栏文本 - 适用于控件但不适用于 MenuItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了以下代码,用于在鼠标悬停时显示控件的 Tag 属性.该代码适用于标签和文本框等标准控件,但我无法让它适用于我的 MenuItems(更具体地说是 ToolStripMenuItems).大家可以看看我的代码并告诉我我做错了什么吗?提前致谢!

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 = "";
}

推荐答案

你的代码有一些问题.

第一.MenuStrip 的 Item 不是 Item 的子项,因此 HasChildren 将返回 false.相反,它们位于 MenuStrip 的 Items 集合中.您需要专门处理 MenuStrip 事件.在下面的 AddEvents... 方法中添加以下代码:

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);
}

并添加新方法如下:

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);
        }
    }
}

第二.ToolStripItem 不是从 Control 派生的,因此在 MouseEnter 中,sender as Control 语句将失败(控件将为空).做这样的事情:

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.

这篇关于在鼠标悬停时设置状态栏文本 - 适用于控件但不适用于 MenuItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在鼠标悬停时设置状态栏文本 - 适用于控件但不适用于 MenuItems

基础教程推荐