Reputation: 23
I am working on a project in Visual Studio 2017, using Winforms and C#. I have created a textbox which accepts only certain characters, and I would like it to display a bubble tooltip saying "Only letters and numbers are allowed" whenever a user enters an invalid character. I have managed to display a tooltip:
//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; //
}
}
But it is shown only if the mouse is over the textbox, and if the textbox is in focus. How do I make a tooltip show over a textbox even if the mouse is not over the control? Thanks in advance.
Upvotes: 1
Views: 763
Reputation: 885
You can use Tooltip control on the form, you can do it like this:
ToolTip1.Show("Text to display", Control)
Check this link for reference.
Upvotes: 1
Reputation: 1193
Consider using System.Windows.Forms.ToolTip.Show Method instead.
Upvotes: 0