Search code examples
c#.nettextboxtooltip

Show tooltip on textbox entry


I have a textbox that requires data to be entered in a certain way. I have implemented some cell validating techniques to check the data after it has been entered, but I'd like to provide the user with some information before they enter the data.

To that end, I'd like to add a tooltip to the textbox that pops up when the user enters the toolbox, then exits when they begin to type.

For example I have the following code:

private void YearEdit_Enter(object sender, EventArgs e)
  {
        ToolTip tt = new ToolTip();
        tt.IsBalloon = true;
        tt.InitialDelay = 0;
        tt.ShowAlways = true;
        tt.SetToolTip(YearEdit, "Enter 4 digit year.");
    }

This executes when the user enters the textbox, however the tooltip only appears when the mouse hovers over the textbox. Does anyone have any ideas to work around this? I thought that perhaps tt.ShowAlways = true might work, but obviously not.


Solution

  • Hook into the textbox.enter event and use the following code:

    private void textBox1_Enter(object sender, EventArgs e)
        {
            TextBox TB = (TextBox)sender;
            int VisibleTime = 1000;  //in milliseconds
    
            ToolTip tt = new ToolTip();
            tt.Show("Test ToolTip", TB, 0, 0, VisibleTime);
        }
    

    Play with X/Y values to move it where you want. Visible time is how long until it disappears.