views:

80

answers:

2

Hi,

We have a number of multiline, scrollable winforms texbox controls in .net 2.0 that need to implement pagination in order to allow the user to navigate their contents only by moving a single page (a full control's worth of text) at a time.

As part of this I need to get the number of full lines of text that can be visible on screen at any point to work out how many pages the textbox contains.

I'm fairly sure there isn't a windows message that will alow me to get this information directly, nor have I been able to work out an acceptible solution to this issue.

Below is the current solution we have implemented, this tends to occasionally give a rounding error of at least one line so it has been less than successful.

public int LinesPerPage
    {
        get 
        {
            return (int)(this.Height / this.Font.Height); 
        }
    }

Ideally I would like to know the algorithm used in drawing text to the control but if this isn't available and other suggestions people have would be greatly appreciated.

Thank you in advance,

Evan

A: 

You could try this:

public int LinesPerPage    
{     
   get   
   {      
      return (int)(this.ClientSize.Height / this.Font.Height);
   }
}
SwDevMan81
I assume there is also some vertical padding between lines of text? Wouldn't that need taken into consideration?
Winston Smith
I tried the above and it seems to work for default textbox and font settings, I'm not sure if you can change the padding between lines, but if you can, then yes
SwDevMan81
A: 

The System.Drawing.Graphics class has a method called MeasureString. One of the overloads to the method returns the lines fitted (what would be visible based on the other parameters passed in).

Joe Caffeine