简体   繁体   English

C# 在禁用的文本框(表单)上显示工具提示

[英]C# Display a tooltip on disabled textbox (Form)

I am trying to get a tooltip to display on a disabled textbox during a mouse over.我试图让工具提示在鼠标悬停期间显示在禁用的文本框上。 I know because the control is disabled the following won't work:我知道因为控件被禁用,以下将不起作用:

private void textBox5_MouseHover(object sender, EventArgs e)
{
       // My tooltip display code here
}

How can I get the tooltip to display on a mouse over of a disabled control?如何在鼠标悬停在禁用控件上时显示工具提示?

Many thanks非常感谢

MouseHover wont fire if control is disabled.如果控制被禁用,则 MouseHover 不会触发。 Instead you can check in Form MouseMove event whether you hover the textbox相反,您可以检查 Form MouseMove 事件是否将鼠标悬停在文本框上

    public Form1()
    {
        InitializeComponent();
        textBox1.Enabled = false;
        toolTip.InitialDelay = 0;
    }

    private ToolTip toolTip = new ToolTip();
    private bool isShown = false;

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if(textBox1 == this.GetChildAtPoint(e.Location))
        {
            if(!isShown)
            {
                toolTip.Show("MyToolTip", this, e.Location);
                isShown = true;
            }
        }
        else
        {
            toolTip.Hide(textBox1);
            isShown = false;
        }
    }

在此处输入图片说明

You can also drag a ToolTip object from the Toolbox in designer onto the form.您还可以将工具提示对象从设计器中的工具箱拖动到表单上。 Then in the code you just call SetToolTip() and pass in the button or text box etc. you want the tool tip to assign to and the text you want it to show.然后在代码中,您只需调用 SetToolTip() 并传入按钮或文本框等。您希望将工具提示分配给您希望它显示的文本。

myToolTip.SetToolTip(myTextBox, "You just hovered over myTextBox!");

Late to the party, but had the same problem and found a better solution: you can just wrap your TextBox in another Item and put a ToolTip on it like:迟到了,但遇到了同样的问题并找到了更好的解决方案:您可以将 TextBox 包装在另一个项目中并在其上放置一个工具提示,例如:

<Grid ToolTip="ToolTip to display">
            <TextBox  IsEnabled="False" Text="Text to display" />
</Grid>

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

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