Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into multiple Textboxes in Windows Phone

Since there's the 2048x2048 pixel limit for UIElements in WPhone, I'm trying to split a string that is too long to be shown.

I've tried implementing the ScrollableTextBlockdone here but to no success. So I'm struggling to do it in another way.

I've tried using a wildcard in the text, specifically \r\n: when it's reached, a method Splitter done with Regex returns the remaining substring:

private string Splitter(string str1)
    {
        MatchCollection matches = Regex.Matches(str1,"\r\n");
        int count = matches.Count / 2; //i want to split every text in half avoiding word truncating, 
        //so I'm getting the NewLine closest to the middle of the text
        int pos= matches[count].Index; //I get the index of the char in str1
        return str1.Substring(pos); 
    }

But it gives me the ArgumentIsNullException when reaching matches[count].

How can I resolve this?

like image 731
riciloma Avatar asked Sep 01 '26 02:09

riciloma


1 Answers

I think you may just need some validation on your argument and on matches.Count:

private static string Splitter(string str1)
{
    if (string.IsNullOrWhiteSpace(str1)) return str1;
    MatchCollection matches = Regex.Matches(str1, "\r\n");

    if (matches.Count == 0) return str1;

    int count = matches.Count / 2;
    int pos = matches[count].Index;
    return str1.Substring(pos);
}
like image 101
Rufus L Avatar answered Sep 03 '26 17:09

Rufus L