yohannist
yohannist

Reputation: 4204

How to find how much text fits in a textbox without scrolling

I have a relatively large text. I need to add a certain amount of this text to a textbox so that it can be visible without scrolling , then add the rest of the text to another textbox and then another -.-.-.> looping through the text generating as many textboxes as necessary.

My problem is i don't know how to find out how much of the text fits in each textbox. So far the only thing i was able to do is assign a fixed number of characters that fit in a page. But this would not do for different screen resolutions. Is there a way, a trick or a workaround i can use to calculate how much of a text can fit into a textbox with fixed font and fontsize but relative width and height?

int TextLength = 1000, PageStart = 0;

List<TextBox> Pages = new List<TextBox>();  
while (PageStart < TextLength) 
{
     TextBox p = new TextBox();
      if (PageStart + PageLength < TextLength)
      {
             p.PageText = Text.Substring(PageStart, PageLength);
             PageStart += PageLength;
             Pages.Add(p);
      }
      else
      {
             PageLength = TextLength - PageStart;
             p.PageText = Text.Substring(PageStart, PageLength);
             Pages.Add(p);
             break;
       }                      
  }

Upvotes: 3

Views: 950

Answers (1)

Filip Skakun
Filip Skakun

Reputation: 31724

You would probably be better of using a TextBlock. Other than that the TextBlock measuring technique should work for TextBoxes too - how to calculate the textbock height and width in on load if i create textblock from code?

You would need to measure ActualHeight while increasing the amount of text until you go over your limit.

Upvotes: 1

Related Questions