简体   繁体   English

当MouseEnter时NumericUpDown不会更改ToolStripStatusLabel

[英]NumericUpDown is not changing ToolStripStatusLabel when MouseEnter

I used this code to implement on hover tooltip , it works with TextBox , ComboBox , MaskedTextBox but not on NumericUpDown . 我使用此代码在悬停tooltip上实现,它与TextBoxComboBoxMaskedTextBox一起使用,但不适用于NumericUpDown Does anybody know why it is not working? 有人知道为什么它不起作用吗?

public static void addHovertip(ToolStripStatusLabel lb, Control c, string tip)
        {

            c.MouseEnter += (sender, e) =>
            {
                lb.Text = tip;
                // MessageBox.Show(c.Name);
            };
            c.MouseLeave += (sender, e) =>
            {
                lb.Text = "";

            };
        }

I admit the deleted answer from Hans Passant helped a bit in creating this answer. 我承认,汉斯·帕桑(Hans Passant)删除的答案在创建此答案方面有所帮助。

First of all your code works fine. 首先,您的代码运行良好。 If you're working with events that happen rather often (like the MouseEvents) you better add a Debug.WriteLine to your code so you can see in the Debugger Output Window which events, for which controls, in which order take place. 如果您要处理经常发生的事件(例如MouseEvents),则最好在代码中添加一个Debug.WriteLine ,这样您就可以在“调试器输出”窗口中看到哪些事件,哪个控件,哪个顺序发生。

The main problem is that due to the fact that the numeric up/down control is a control that is buildup with two different child controls your MouseLeave event is called as soon as the mouse enters one of the two child controls. 主要问题在于,由于数字上/下控件是由两个不同的子控件组成的控件,因此一旦鼠标进入两个子控件之一,就会调用MouseLeave事件。 What happens is: MouseEnter is called when the mouse hits the single line border of the control and MouseLeave is called the moment the mouse is no longer on that line. 发生的情况是:当鼠标击中控件的单行边框时,将调用MouseEnter;当鼠标不再位于该行上时,将调用MouseLeave。 In MouseLeave you set the Label to an emtpy string. 在MouseLeave中,将Label设置为空字符串。 That gives the impression that your code doesn't work. 这给人的印象是您的代码不起作用。

By simply adding a loop to go over any child controls solves this issue. 通过简单地添加循环以遍历任何子控件即可解决此问题。 This still set the label to an empty string a bit to often but it is also immediately set to the correct text if needed. 这仍然经常将标签设置为空字符串,但是如果需要的话,也会立即将其设置为正确的文本。

Here is the changed code with the Debug statements in place. 这是更改后的代码,并带有Debug语句。

    public static void addHovertip(ToolStripStatusLabel lb, Control c, string tip)
    {
        c.MouseEnter += (sender, e) =>
        {
            Debug.WriteLine(String.Format("enter {0}", c));
            lb.Text = tip;
        };

        c.MouseLeave += (sender, e) =>
        {
            Debug.WriteLine(String.Format("Leave {0}", c));
            lb.Text = "";
        };

        // iterate over any child controls
        foreach(Control child in c.Controls)
        {
            // and add the hover tip on 
            // those childs as well
            addHovertip(lb, child, tip);
        }
    }

For completeness here is the Load event of my test form: 为了完整起见,这是我的测试表单的Load事件:

 private void Form1_Load(object sender, EventArgs e)
 {
     addHovertip((ToolStripStatusLabel) statusStrip1.Items[0], this.numericUpDown1, "fubar");
 }

Here is an animated gif demonstrating what happens when you move the mouse in and out the Numeric Up Down control: 这是一个动画gif,演示了将鼠标移入和移出Numeric Up Down控件时发生的情况:

数字上向下控制和鼠标事件调试输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM