Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
       }                      
  }
like image 876
yohannist Avatar asked Jan 17 '26 17:01

yohannist


1 Answers

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.

like image 200
Filip Skakun Avatar answered Jan 20 '26 07:01

Filip Skakun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!