You could do something like the following if you don't care about overwriting
existing tooltips:
private ToolTip tt = new ToolTip();
private Graphics g;
public MyConstructor() {
// If you use MouseHover instead of MouseEnter, it will take 2 hovers
// in order to show initially. TextChanged lets it update as you type.
// Unfortunately, ToolTip only checks to see if it should display when you
// hover over the control, so if the text is short enough to not display
initially,
// hovering and then increasing the text length won't cause the ToolTip to
// display until you move the mouse again.
this.textBox1.TextChanged += new EventHandler(TextBox_ToolTipUpdater);
this.textBox1.MouseEnter += new EventHandler(TextBox_ToolTipUpdater);
tt.InitialDelay = 0;
tt.AutoPopDelay = int.MaxValue;
g = Graphics.FromHwnd(this.Handle);
}
private void TextBox_ToolTipUpdater(object sender, EventArgs e) {
TextBox t = sender as TextBox;
if (t != null && t.Text != tt.GetToolTip(t)) {
SizeF textSize;
textSize = g.measureString(t.Text, t.Font);
if (textSize.Width > (float)t.Size.Width) {
tt.SetToolTip(t, t.Text);
} else {
tt.SetToolTip(t, string.Empty);
}
}
}
"melanieab" wrote:
Hi,
Is there an easy way to say, on textboxHover, if the text inside the box
isn't completely visible, have a popup tool come up showing the complete
contents of the box? I'm guessing I need to know if there's a way to
determine the number of pixels that some generic text takes up. Also, is
there a way to extract the pixel length from textbox.Size (to save some time
looking values up)?
Thanks so much!!!
Mel