How to get the size of the displayed text in the text box

I need a way to determine the size of the displayed text in multi-column TextBoxto set the property Scrollbarsto the correct value.

Since this is some kind of extended MessageBoxone that I'm working on, the size MessageBoxshould be determined by the height and width of the text, the source of which is lines with newline characters.

I am currently using this code to determine the size MessageBoxdepending on the input text. But you see that is MessageBoxdetermined MaximiumSize. TextBox for the text itself also has WordWrap. So, the only thing undefined is the Heighttext after it is inserted into the TextBox.Text.

SizeF textSize = this.tbxText.CreateGraphics().MeasureString(message, this.tbxText.Font);

int frmWidth = picWidth + (int)textSize.Width;
if (frmWidth > this.MaximumSize.Width)
{
    frmWidth = this.MaximumSize.Width;
}
else if (frmWidth < this.MinimumSize.Width)
{
    frmWidth = this.MinimumSize.Width;
}

int frmHeight = picHeight + (int)textSize.Height + pnlButtons.Height + pnlInput.Height;
if (frmHeight > this.MaximumSize.Height)
{
    frmHeight = this.MaximumSize.Height;
}
else if (frmHeight < this.MinimumSize.Height)
{
    frmHeight = this.MinimumSize.Height;
}

TextBox.Scrollbars " " , . , Graphics.MeasureString , WordWrap.

, , TextBox.Text , ?

+3
5

, RichTextBox, , Scrollbars, , TextBox.

RichTextBox Scrollbars , .

+1

Graphics.MeasureString, , , (.. , ), , .

, , Graphics.MeasureString , - , .

+2

AutoSize, / , , .

+1

Graphics.MeasureString width ( ).

, , , :

SizeF textSize = this.tbxText.CreateGraphics()
    .MeasureString(message, this.tbxText.Font, this.tbxText.Width);
...

Graphics.MeasureString .: MeasureString (MSDN)

0

. UserControl , , , . ( - , , , - .)

Load Load :

int iLine = textbox1.GetLineFromCharIndex(textbox1.TextLength - 1) + 1;
int iHeight = TextRenderer.MeasureText(this.textbox1.Text, this.textbox1.Font).Height;
float fTextHeight = iHeight * ((float)iLine + 0.25f /* fudge factor */);
textbox1.Size = new Size (textbox1.Size.Width, (int)fTextHeight);

TextRenderer.MeasureText().

0

Source: https://habr.com/ru/post/1754418/


All Articles