How to get the real height of a TextBox?

My first thought was this:

int height = textbox.lines.length * lineheight;

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

+5
source share
2 answers

I don’t know if you ever get the perfect measurement, but it’s close:

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

TextBox can be dumb. When you turn on the multi-line line, if you click on the character that calls the word word-wrap, clicking on the backspace will not produce "un-word-wrap" unless I resize the TextBox. It was on Win7-64. I do not think the TextBox control has always done this.

+6
source

GetLineFromCharIndex() , .

int lineCount = textBox1.GetLineFromCharIndex(int.MaxValue) + 1;
int lineHeightPixel = TextRenderer.MeasureText("X", textBox1.Font).Height;
0

All Articles