3

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;
       }                      
  }
svick
  • 236,525
  • 50
  • 385
  • 514
yohannist
  • 4,166
  • 3
  • 35
  • 58
  • 2
    You'd probably need to use monospace fonts for this to work, or if not you'd need to do some pretty complex maths :/ – It'sNotALie. Feb 03 '13 at 19:25
  • monospace will have to do if i don't find anything else. i think the calculation is simple. – yohannist Feb 03 '13 at 19:34
  • You'd need to deal with every charachter individually with non-monospace. Also, a bunch of monospace fonts: http://www.fontsquirrel.com/fonts/list/style/Monospaced – It'sNotALie. Feb 03 '13 at 19:45
  • @ofstream Not necessarily. I believe there's some precedent to allow for just measuring the width in `em`s (which should be the widest letter in the font.) So if you know the textbox can fit 20 `M`s in whatever font it has, it's very likely it can fit at least 20 of any letter in said font. To simplify it even further, the [Wikipedia article](http://en.wikipedia.org/wiki/Em_(typography)) seems to imply that the bounding box for an `M` is a rectangle with the point size of the font. It seems a good guesstimate/lower bound might be the width of the textbox divided by the font height. – millimoose Feb 03 '13 at 20:22
  • @fire'fly For a more precise computation, what you're looking for is the *advance* of a string in a given font, but I'll be damned if I can find a straightforward way to compute that in .NET. Poke around in [`System.Windows.Media`](http://msdn.microsoft.com/en-us/library/ms608873.aspx) if that's available in WinRT. – millimoose Feb 04 '13 at 08:45

1 Answers1

1

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.

Community
  • 1
  • 1
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100