camino
camino

Reputation: 10594

how to check whether a string can fit into richtextbox visible area

I want to divide a very large string into slices so that each slice can fit into the visible area of a richtextbox so that the horizontal scroll bar will not show.

I would like to know how to determine whether a string can fit into the richtextbox's visible area without changing the richtextbox's value.

I searched for the solution and most results I found suggest that MeasureText / MeasureString should be used, but it seems these two function assume there is only one line(not wrapped).

Is there any way to find out the height of a multiple line string which will be set into a richtextbox?

Upvotes: 0

Views: 207

Answers (1)

kgzdev
kgzdev

Reputation: 2885

MeasureText and MeasureString functions has overloads that accept textarea width, height and TextFormatFlags(TextBoxControl, WordBreak e.t.c)

You can compare your text size with RichTextBoxControl size

Size stringSize = new Size();
string text = SomeText();
stringSize = TextRenderer.MeasureText(text, richTextBox1.Font, new Size(richTextBox1.Size.Width, richTextBox1.Size.Height), TextFormatFlags.WordBreak);
string r = string.Format("RTB Width : {0}\r\n", richTextBox1.Size.Width);
r += string.Format("RTB Height : {0}\r\n", richTextBox1.Size.Height);
r += string.Format("TEXT Width : {0}\r\n", stringSize.Width);
r += string.Format("TEXT Height : {0}", stringSize.Height);
MessageBox.Show(r);

Upvotes: 1

Related Questions