when text is longer than its textbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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);
}
}
}
 
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


You don't say if you have a multiline or a single line text box. This
seems to work for a single line textBox:

private void myTextBox_MouseHover(object sender, EventArgs e) {
Point pStart = myTextBox.GetPositionFromCharIndex(0);
Point pEnd =
myTextBox.GetPositionFromCharIndex(myTextBox.Text.Length - 1);

if (pStart.X > myTextBox.Width || pEnd.X > myTextBox.Width) {
MessageBox.Show(myTextBox.Text, this.Text);
} // end if
}

When the start of the text is not showing, the return from
GetPositionFromCharIndex() for pStart.X seems to be 65394. The code
relies on the text box width being less than this value.

An alternative might be to use a ToolTip to display the text rather
than a MessageBox.


rossum
 
Back
Top