自定义工具提示无法正常工作
Customized ToolTip Not Working Correctly
在我的 Windows C# 表单应用程序中,我有许多 TextBox
控件,所有这些控件都附加了相同的 ToolTip
control/message。没有任何定制,ToolTip
运行完美。
现在,我使用在 Change winform ToolTip backcolor 中选为最佳答案的代码片段将 BackColor
添加到 ToolTip
气球。这对于将 BackColor
添加到 ToolTip
气球确实很有效,但它以某种方式删除了字符串消息中的所有 Environment.NewLine
。不过,它似乎显示了相同大小的气球。
谁能告诉我为什么会这样,以及如何解决这个问题?
private ToolTip _tt = new ToolTip();
private string _ttipText;
private void ToolTipCustomization(){
string nl = Environment.NewLine;
/* This text is not shown properly when BackColor is added */
_ttipText = "The value must be: " + nl +
"1, Numeric " + nl +
"2, Between 0 and 1000" + nl +
"3, A multiple of 10";
_tt.OwnerDraw = true;
_tt.BackColor = Color.LightBlue;
_tt.Draw += new DrawToolTipEventHandler(TT_Draw);
}
private void TT_Draw(object sender, DrawToolTipEventArgs e){
e.DrawBackground();
e.DrawBorder();
e.DrawText();
}
//Adding TextBox controls programmatically
private Textbox[] tbx = new TextBox[20];
private void CreateTextBox(){
for(int i=0; i<20; i++){
tbx[i] = new TextBox();
/* More TextBox properties for design (Omit) */
_tt.SetToolTip(tbx[i], _ttipText); //Set ToolTip text to tbx here
this.Controls.Add(tbx[i]);
}
}
我尝试订阅 PopupEventHandler
,其中我放大了气球的大小,但它没有解决我的问题。
最终,由于 Hans Passant.
的建议,我找到了自己问题的解决方案
就像在 e.DrawText
中添加一个 TextFormatFlags
参数一样简单,如下所示:
private void TT_Draw(object sender, DrawToolTipEventArgs e){
e.DrawBackground();
e.DrawBorder();
e.DrawText(TextFormatFlags.VerticalCenter); /* here */
}
现在可以正常显示文字了。谢谢!
在我的 Windows C# 表单应用程序中,我有许多 TextBox
控件,所有这些控件都附加了相同的 ToolTip
control/message。没有任何定制,ToolTip
运行完美。
现在,我使用在 Change winform ToolTip backcolor 中选为最佳答案的代码片段将 BackColor
添加到 ToolTip
气球。这对于将 BackColor
添加到 ToolTip
气球确实很有效,但它以某种方式删除了字符串消息中的所有 Environment.NewLine
。不过,它似乎显示了相同大小的气球。
谁能告诉我为什么会这样,以及如何解决这个问题?
private ToolTip _tt = new ToolTip();
private string _ttipText;
private void ToolTipCustomization(){
string nl = Environment.NewLine;
/* This text is not shown properly when BackColor is added */
_ttipText = "The value must be: " + nl +
"1, Numeric " + nl +
"2, Between 0 and 1000" + nl +
"3, A multiple of 10";
_tt.OwnerDraw = true;
_tt.BackColor = Color.LightBlue;
_tt.Draw += new DrawToolTipEventHandler(TT_Draw);
}
private void TT_Draw(object sender, DrawToolTipEventArgs e){
e.DrawBackground();
e.DrawBorder();
e.DrawText();
}
//Adding TextBox controls programmatically
private Textbox[] tbx = new TextBox[20];
private void CreateTextBox(){
for(int i=0; i<20; i++){
tbx[i] = new TextBox();
/* More TextBox properties for design (Omit) */
_tt.SetToolTip(tbx[i], _ttipText); //Set ToolTip text to tbx here
this.Controls.Add(tbx[i]);
}
}
我尝试订阅 PopupEventHandler
,其中我放大了气球的大小,但它没有解决我的问题。
最终,由于 Hans Passant.
的建议,我找到了自己问题的解决方案就像在 e.DrawText
中添加一个 TextFormatFlags
参数一样简单,如下所示:
private void TT_Draw(object sender, DrawToolTipEventArgs e){
e.DrawBackground();
e.DrawBorder();
e.DrawText(TextFormatFlags.VerticalCenter); /* here */
}
现在可以正常显示文字了。谢谢!