471,317 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

when text is longer than its textbox

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
Dec 13 '05 #1
2 4706
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

Dec 13 '05 #2
On Tue, 13 Dec 2005 13:42:43 -0800, melanieab
<me*******@discussions.microsoft.com> 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

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

--

The ultimate truth is that there is no ultimate truth
Dec 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Luis Esteban Valencia | last post: by
23 posts views Thread by Paul Mars | last post: by
16 posts views Thread by mj.redfox.mj | last post: by
2 posts views Thread by =?Utf-8?B?TE1X?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.