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 that 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 one -.-.-. > Loop through the text, generating as many text fields as needed.
My problem is that I don't know how to find out which part of the text fits in each text box. So far, the only thing I have been able to do is assign a fixed number of characters that fit into the page. But that wouldn't be the case for different screen resolutions. Is there a way, trick, or workaround that I can use to figure out how much of the text can fit in a text box with fixed font and fonts, 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;
}
}
source to share
You are probably better off using TextBlock. Also, the TextBlock method should work for TextBoxes as well - how to calculate the height and width of the textblock on load if I create the textblock from code?
You will need to measure ActualHeight by increasing the amount of text until you hit your limit.
source to share