【发布时间】: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