Search code examples
c#uwpricheditbox

How to get First Char Index From Line in RichEditbox (UWP)?


I am trying to find a way to determine the Index of the first character in a specific Line in RichEditBox (UWP)

in Richtextbox was easy

int indx1stlinchr = myRichTextBox.GetFirstCharIndexFromLine(i);

is there a way to do that or a workaround method ?

a part from my coloring method in Winforms Richtextbox

       public void colorTheText(string rohtext)
    {
        myRichTextBox.SelectionLength = 0;
        int def = network.SubSop_Deff;
        var line = Regex.Split(rohtext, "\n|\r|\n\n");
        int ipclassRange = CColor.KlassebitsRange(network.Network_Class, network.Netmask_length);
        int k = 1;
        int l = 1;

        for (int i = 0; i < line.Length; i++)
        {

            int indx1stlinchr = myRichTextBox.GetFirstCharIndexFromLine(i);
            int indexlinedge = line[i].LastIndexOf("  ");


            if (line[i].StartsWith(">Network") == true)
            {
                myRichTextBox.SelectionLength = 0;
                myRichTextBox.Select(indx1stlinchr, 12);
                myRichTextBox.SelectionColor = Color.Black;
                myRichTextBox.Select((indx1stlinchr + 13), 19);
                myRichTextBox.SelectionColor = Color.Blue;
                if (network.Reserved_IP != null)
                {
                    myRichTextBox.SelectionLength = 0;
                    myRichTextBox.Select((indx1stlinchr + 76), 13);
                    myRichTextBox.SelectionColor = Color.Green;
                }
                if (ipclassRange > 0)
                {

                    myRichTextBox.Select(indx1stlinchr + 39, ipclassRange);
                    myRichTextBox.SelectionColor = Color.Green;
                }

                myRichTextBox.SelectionLength = 0;

            }

            if (line[i].StartsWith("Netmask") == true)
            {
                myRichTextBox.SelectionLength = 0;
                myRichTextBox.Select(indx1stlinchr, 19);
                myRichTextBox.SelectionColor = Color.Black;
                myRichTextBox.Select((indx1stlinchr + 13), 19);
                myRichTextBox.SelectionColor = Color.Blue;
                myRichTextBox.Select((indx1stlinchr + 38), 38);
                myRichTextBox.SelectionColor = Color.Red;
                myRichTextBox.SelectionLength = 0;

                if (super == false)
                {
                    indxlinstrt = myRichTextBox.GetFirstCharIndexOfCurrentLine();

                    if (k == 1)
                    {
                        myRichTextBox.Select(indxlinstrt + indexlinedge, (def + 4) * -1);
                        subrange = CColor.FindeSubRange(Regex.Split(myRichTextBox.SelectedText, ""), network.Netmask_length, def);
                        k = 0;
                    }

                    myRichTextBox.Select(indxlinstrt + indexlinedge, subrange);
                    myRichTextBox.SelectionColor = Color.DarkViolet;
                    myRichTextBox.SelectionLength = 0;
                }

            }//.........etc

        }
        super = false;
        k = 1;
    }

Solution

  • Try this snippet:

     private int GetFirstCharIndexOfLine(ITextDocument document, int line)
        {
            string value;
            document.GetText(TextGetOptions.None, out value);
    
            int size = 0;            
    
            while (value.Length > size)
            {
                ITextRange range = document.GetRange(size, size + 1);
    
                size += range.Expand(TextRangeUnit.Line);
    
                var lineIndex = range.GetIndex(TextRangeUnit.Line);
    
                if (line == lineIndex)
                {
                    //start of range is first line character index
                    return range.StartPosition;
                }
    
                size += 1;
    
            }
    
            return -1;
        }