Windows Develop Bookmark and Share   
 index > Windows Forms General > Lines per page in Rich text box
 

Lines per page in Rich text box

Hi everyone,

Does anyone know how to get the lines per page in a textbox? By that I mean the number of lines of text that is visible to the user.

I tried:

int LinesPerPage = (int) (richtextbox.Height / richtextbox.Font.Height);

but it's always a little off.

I'm using Courier New font (sorry not changeable) and 11 pixel font size (changeable).

Thanks,
Rick
  • Moved byRoahn LuoMSFTWednesday, July 22, 2009 10:55 AMfor windows Forms controls (From:Visual C# General)
  •  
Rick Delhommer  Monday, July 20, 2009 11:33 PM

Hi Rick,

We can retrieve the height of the total text area by calling the MeasureText method of the TextRenderer class. Then we can use this height to calculate a rate and get the visible lines.

This is the code snippet:
//Get the height of the text area.

int height = TextRenderer.MeasureText(this.richTextBox1.Text,this.richTextBox1.Font).Height;

//rate = visible height / Total height.

float rate = (1.0f * this.richTextBox1.Height) / height;

//Get visible lines.

int visibleLines = (int)(this.richTextBox1.Lines.Length * rate);

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 22, 2009 11:45 AM

Hi Rick,

We can retrieve the height of the total text area by calling the MeasureText method of the TextRenderer class. Then we can use this height to calculate a rate and get the visible lines.

This is the code snippet:
//Get the height of the text area.

int height = TextRenderer.MeasureText(this.richTextBox1.Text,this.richTextBox1.Font).Height;

//rate = visible height / Total height.

float rate = (1.0f * this.richTextBox1.Height) / height;

//Get visible lines.

int visibleLines = (int)(this.richTextBox1.Lines.Length * rate);

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Wednesday, July 22, 2009 11:45 AM
abcdefghijklmnopqr;
stuvwxyz01234567;
89;
abcdefghijklmnopqr;
stuvwxyz01234567;
89;


  int I = 0;
  int J = 0;
  int K = 0;
  int PhysLine = 0;
  int OldPhysLine = 0;
  OldPhysLine = -1;
  for (I = 0; I < RichTextBox1.Lines.Length; I++)
  {
	for (J = 0; J < RichTextBox1.Lines(I).Length; J++)
	{
	  PhysLine = RichTextBox1.GetLineFromCharIndex(K);
	  if (PhysLine != OldPhysLine)
	  {
		Console.Write("Line no = " + I.ToString());
		Console.Write(" Phys line no = " + PhysLine.ToString());
		Console.WriteLine(" Char index = 0" + K.ToString());
		OldPhysLine = PhysLine;
	  }
	  K += 1;
	}
  }
Line no = 0 Phys line no == 0 char index == 00;
Line no = 0 Phys line no == 1 char index == 018;
Line no = 0 Phys line no == 2 char index == 034;
Line no = 1 Phys line no == 3 char index == 037;
Line no = 1 Phys line no == 4 char index == 055;
Line no = 1 Phys line no == 5 char index == 071;
JohnWein  Wednesday, July 22, 2009 1:48 PM
Worked like a charm. Thanks Aland. Could you explain to me though how it works? (Sorry this is my first ever c# project lol)

Here's what I see:
1) calculate the visible text area (I guess richtextbox.Height is different from the Height of area where the visible text is?)
after that I kind of get lost on the next two statements.

Rick
Rick Delhommer  Wednesday, July 22, 2009 10:32 PM

Hi Rick Delhommer,

I would explain the two areas at first, then explain how I use them to calculate the visible lines.

The text areas:

1. Visible text area: The height of the RichTextBox. We would see the visible lines in this area. For example, we can see nine lines in this area if the 10th line is hidden under the bottom edge of the RichTextBox.

2. Total text area: The height of the area in which all the lines can be displayed. TextRender can draw text on a control or a form. It can also measure the text. In most cases, before we draw the text, we can use MeasureText to get a rectangle so that all the characters in the text can be displayed in this area. Then we can allocate enough spaces to display the text. For example, if there are 13 lines in the text of the RichTextBox, the rectangle returned by MeasureText can display all the 13 lines.

These are the steps to calculate the visible lines:

1. Get the height of the visible text area and the total text area.

2. Calculate the visible rate: visible height / total height. In other words, rate = visible lines / total lines.

3. Get the total lines and calculate the visible lines by using this formula: visible lines = total lines * rate.

You can get more information about TextRender from
http://msdn.microsoft.com/en-us/library/system.io.textreader.aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, July 23, 2009 2:06 AM
Got it. Thanks for the explanation

Rick
Rick Delhommer  Thursday, July 23, 2009 9:24 PM
Hey Aland,

I have a problemfor which there seems no example on the Internet.

I have a UI with a RichTextBox that has text added regularly. If the text being added is more than a "visible page", then the UI should pause, put up a "more" prompt and wait for the user to press a key before continuing to add/display the rest of the text. There could potentially be several more prompts in a given text-add.

Your TextRenderer example seems to hint at a solution, but I'm not sure where to go...

So imagine I have

RichTextBox TextWindow = new RichTextBox();

and then a method...

private void AddNewText(string text) {


}

Any idea how to make the AddNewText function with a more prompt?

I'm stumped

David C.
ChicagoDave  Friday, September 18, 2009 1:13 PM

You can use google to search for other answers

Custom Search

More Threads

• Outlook
• Prevent to run same exe moretimes in same mahine
• What xml translator is WebBrowser using?
• a simple question about hiding a form
• DataGridView.Sort question
• Richtextbox extended with common formatting buttons
• Add Control to UserControl
• Load TreeView from database
• treeview child nodes collapsed by default
• Generate C++ classes from XML schema