一个按钮显示工具提示时,文本太长
问题描述:
我有在WinForm 按钮文本长度的按钮可能的各种操作过程中非常..一个按钮显示工具提示时,文本太长
我不想改变按钮的大小(所以我已经设置“Autosize”属性为false)
如何在鼠标悬停时显示工具提示(完整按钮文本),只要按钮文本正在切割?
请注意,我不想总是提示.....我想它,只有当按钮上的文字是越来越切
答
希望这个代码将有助于您
if (button1.Text.Length > Your button text length to be checked)
{
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip(this.button1, this.button1.Text);
}
您必须在按钮写那些代码MouseHover事件
答
我认为你必须手动检查文本的长度上按钮,按钮
的大小,如果大于你必须添加按钮运行时的提示财产
不要忘记从工具箱拖动
感谢
在你的项目中添加工具提示控制
答
替代方法:使用按钮AutoElipsis属性为True。
答
我不认为到目前为止的答案是非常正确的 - 呈现的字符串的长度(并且这也是您在考虑按钮尺寸时需要的)的长度可能会因字体和字符而异使用。使用比例字体诸如Microsoft Sans Serif
将返回不同的尺寸用于容纳相同数量的字符的字符串时,这些字符不同,例如:
“IIIIIIIIII”不是一样宽
“wwwwwwwwww”。
你应该使用`Graphics类
Graphics grfx = Graphics.FromImage(new Bitmap(1, 1));
// Set a proportional font
button1.Font = new Font("Microsoft Sans Serif", 8.25f, FontStyle.Regular);
SizeF bounds = grfx.MeasureString(
button1.Text,
button1.Font,
new PointF(0, 0),
new StringFormat(StringFormatFlags.MeasureTrailingSpaces));
MessageBox.Show("Text dimensions: " + bounds.Width + "x" + bounds.Height);
// Set a non-proportional font
button1.Font = new Font("Courier New", 8.25f, FontStyle.Regular);
bounds = grfx.MeasureString(
button1.Text,
button1.Font,
new PointF(0, 0),
new StringFormat(StringFormatFlags.MeasureTrailingSpaces));
MessageBox.Show("Text dimensions: " + bounds.Width + "x" + bounds.Height);
这个伟大的工程的
MeasureString
方法,有一两件事我注意到的是,如果该按钮启用它不会显示。发现了这一点,因为我想说出为什么一个按钮被禁用的原因。 – DanO 2014-01-24 19:31:42