Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TextBox's real height?

My first thought was it would be something like this:

int height = textbox.lines.length * lineheight;

But it just counts "\xd\n" and lines can be wrapped. Can i get the number of displayed lines or actual height of textbox when everything is visible(the height of text inside)?

like image 752
Mike Avatar asked Feb 12 '12 14:02

Mike


1 Answers

I don't know if you will ever get a perfect measurement, but this gets close:

private int GetTextHeight(TextBox tBox) {
  return TextRenderer.MeasureText(tBox.Text, tBox.Font, tBox.ClientSize,
           TextFormatFlags.WordBreak | TextFormatFlags.TextBoxControl).Height;
}

The TextBox can be goofy. With multi-line turned on, if you press a character that causes the word to word-wrap, hitting backspace does not cause it to "un-word-wrap" unless I resize the TextBox. This was on a Win7-64. I don't think the TextBox control always did that.

like image 78
LarsTech Avatar answered Oct 05 '22 22:10

LarsTech