简体   繁体   中英

C# Winforms - Display Tooltip over textbox when mouse not over it

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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