C# 在文本框条目上显示工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14695357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show tooltip on textbox entry
提问by Lukas Bystricky
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.
我有一个textbox
要求以某种方式输入数据。我已经实施了一些单元格验证技术来在输入数据后检查数据,但我想在用户输入数据之前向他们提供一些信息。
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.
为此,我想将添加tooltip
到textbox
当用户进入工具箱弹出,然后当他们开始键入退出。
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.
这在用户输入 时执行textbox
,但是tooltip
只有当鼠标悬停在 上时才会出现textbox
。有没有人有任何想法来解决这个问题?我认为这也许tt.ShowAlways = true
可行,但显然不行。
采纳答案by Lee Harrison
Hook into the textbox.enter event and use the following code:
钩入 textbox.enter 事件并使用以下代码:
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.
使用 X/Y 值将其移动到您想要的位置。可见时间是它消失之前的时间。
回答by ChrisF
Tooltips only appear when the mouse is still by design.
工具提示仅在鼠标仍按设计使用时出现。
You could try setting the InitialDelay
to 0:
您可以尝试将 设置InitialDelay
为 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.
如果你真的想要一个工具提示,那么你可以添加一个信息图标(通常是一个“i”),当它悬停在上面时会显示工具提示,或者实现你自己的。
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.
如果您将日期分成几部分(单独的日、月、年),它也可能起作用。这将允许您更好地控制用户可以输入的内容 - 月份可以成为下拉/组合框,因此它始终是正确的格式。
回答by lamiinek
you can show a tooltip also like this:
您也可以像这样显示工具提示:
ToolTip t = new ToolTip();
t.Show("Hello World", textBox1, 1000);
回答by user2283985
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);
}
}
回答by Gilles Mignard
You should use if ( o.Name == label1.Name)
instead of if ( o.Name == "label1")
, for if you rename label1, this line will be modified too.
你应该使用 if ( o.Name == label1.Name)
而不是 if ( o.Name == "label1")
,因为如果你重命名 label1,这一行也将被修改。
More : if(o.equals(label1))
...
更多:if(o.equals(label1))
...