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

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

问题描述

我设计了下面的代码,用于在mouseover上显示控件的Tag属性。该代码适用于标准和TextBoxes等标准控件,但我无法让它适用于我的MenuItems(更具体地说是ToolStripMenuItems)。你们可否请看看我的代码,并告诉我我做错了什么?

  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(ctrl.Controls中的控件控件)
{
control.MouseEnter + = new EventHandler(this.control_MouseEnter);
control.MouseLeave + = new EventHandler(this.control_MouseLeave);
if(control.HasChildren)
addEventsToAllComponents(control);



$ b 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 =;
}


解决方案

你的代码。

1st。 MenuStrip的Items不是Item的子项,所以HasChildren将返回false。相反,它们位于MenuStrip的Items集合中。你需要特别处理一个MenuStrip事件。
在下面的AddEvents ...方法中添加以下代码:

 (snip ...)
//旧代码
if(control.HasChildren)
AddEventsToAllControls(control);
//在
下面添加新的代码if(control为MenuStrip){
MenuStrip ms =控制为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是ToolStripMenuItem){
ToolStripMenuItem mi = tsi作为ToolStripMenuItem;
AddEventsToAllToolStripMenuitems(mi.DropDownItems);
}
}
}

第二。 ToolStripItem不是从Control派生的,所以在MouseEnter中, sender作为Control 语句将失败(控件将为null)。做这样的事情:

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

解决方案

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆