Show tooltip on textbox entry

2019-02-03 08:10发布

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.

4条回答
叼着烟拽天下
2楼-- · 2019-02-03 09:03

Tooltips only appear when the mouse is still by design.

You could try setting the InitialDelay to 0:

tt.InitialDelay = 0;

But this would still require the mouse to be stationary for an instant.

However there are other approaches. A common way of showing what input is required is to use a watermark (faded text) in the textbox that displays the formatting required until the user starts typing.

If you really want a tooltip then you could either add an information icon (usually an "i") which will show the tooltip when it's hovered over, or implement your own.

It might also work if you break the date into parts (separate day, month, year). This will allow you more control over what the user can enter - the month can become a drop down/combo box so it's always the correct format.

查看更多
beautiful°
3楼-- · 2019-02-03 09:05

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.

查看更多
相关推荐>>
4楼-- · 2019-02-03 09:07

you can show a tooltip also like this:

ToolTip t = new ToolTip();
t.Show("Hello World", textBox1, 1000);
查看更多
淡お忘
5楼-- · 2019-02-03 09:15

Try this. (based on an answer above) Add event handlers for all controls that you want to have a ToolTip for. Point all the event handlers to the same method. Then construct you handling method like this

private void procToolTips(object sender, EventArgs e)
{
   ToolTip tt = new ToolTip();
   Control o = (Control)sender;
   if ( o.Name == "label1") {
     tt.Show("Lorem ipsum dolor sit ame", o, 1000);
   }
}
查看更多
登录 后发表回答