【问题标题】:C# Winforms - Display Tooltip over textbox when mouse not over itC# Winforms - 当鼠标不在文本框上时在文本框上显示工具提示
【发布时间】:2019-01-24 07:31:57
【问题描述】:

我正在使用 Winforms 和 C# 在 Visual Studio 2017 中开发一个项目。我创建了一个只接受某些字符的文本框,并且我希望它在用户输入无效字符时显示一个气泡工具提示,上面写着“只允许字母和数字”。我设法显示了一个工具提示:

//titleInput - The textbox.
//characterWarning - the tooltip.

private void TitleInputKeyPress(object sender, KeyPressEventArgs e)
{
    if (!Char.IsLetter(e.KeyChar) && !Char.IsNumber(e.KeyChar) && !Char.IsWhiteSpace(e.KeyChar) && !Char.IsControl(e.KeyChar)) //Check if character is invalid.
    {
        characterWarning.SetToolTip(titleInput, "Only letters and numbers are allowed"); //Display the tooltip.
        e.Handled = true; //
    }
}

但只有当鼠标在文本框上并且文本框处于焦点时才会显示。 即使鼠标不在控件上方,如何在文本框上显示工具提示?提前致谢。

【问题讨论】:

    标签: c# visual-studio winforms textbox visual-studio-2017


    【解决方案1】:

    你可以在表单上使用Tooltip控件,你可以这样做:

    ToolTip1.Show("Text to display", Control)
    

    查看this link 以供参考。

    【讨论】:

      【解决方案2】:

      考虑改用System.Windows.Forms.ToolTip.Show 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-03
        • 2011-12-14
        • 1970-01-01
        • 2012-07-17
        • 2011-02-28
        相关资源
        最近更新 更多