Home > Enterprise >  How to implement GetLineStartPosition for a WPF TextBox UI Control?
How to implement GetLineStartPosition for a WPF TextBox UI Control?

Time:07-22

How to implement the RichTextBox API, GetLineStartPosition, for a WPF TextBox UI Control?

var charpos_x1 = GetLineStartPosition(0); // get charpos start of the current line
var charpos_x2 = GetLineStartPosition(1); // get charpos start of the next line
var charpos_x3 = GetLineStartPosition(-1); // get charpos start of the prev line

CodePudding user response:

A TextPointer is just a reference to a location in the content of the rich text. TextBox doesn’t expose that but you can use an integer representing the offset of the character in the text. So based on the docs for GetLineStartPosition I think something like the following may work:

    public int? GetLineStartPosition(this TextBox tb, int charIndex, int count)
    {
       /// get the line # for character idx then adjust the line 
       /// number by the specified # of lines
       var l = tb.GetLineIndexFromCharacterIndex(charIndex)   count;
       
       /// if its not valid return null
       if (l < 0 || l >= tb.LineCount) return null;
       
       /// otherwise return the character index of the start 
       /// of the adjusted line
       return tb.GetCharacterIndexFromLineIndex(l);
    }

Then just use it like this:

    tb.GetLineStartPosition(tb.CaretIndex, 0);

CodePudding user response:

        public struct LinePos_t
        {
            public int    indexz;  // Multiline TextBox.Line End-Position
            public int    index0;  // n-Line Begin Position
            public int    index1;  // n-Line End Position
            public int    len;     // n-Line Length
            public string str;     // n-line String
        };

        private static LinePos_t TextBoxMultiline_GetLinePosition(
             TextBox tb, 
             int     n
             )
        {
            // (n == 0):  current Line
            // (n == 1):  next line
            // (n == -1): prev line

            // Notes: end of line marked by: "\r\n" for Textbox.Text
            // User can never postion Cursor on '\n' character...
            // but can position it on \r character 

            LinePos_t linepos = new LinePos_t();
            int       anchor;
            int       repeat;
            string    c;

            tb.Focus();

            if (tb.Text.Length == 0)
            {
                linepos.indexz = 0;
                linepos.index0 = 0;
                linepos.index1 = 0;
                linepos.len    = 0;
                linepos.str    = "";
                return linepos;
            }

            // Position End of Text
            linepos.indexz = tb.Text.Length;
            if (linepos.indexz != 0)
                linepos.indexz--;

            // Position Init Carrot
            if (tb.CaretIndex > linepos.indexz)
                anchor = linepos.indexz;
            else
                anchor = tb.CaretIndex;

            // Rewind when n is negative
            if (n < 0)
            {
                repeat = -n;
                for (int i = 0; i < repeat; i  )
                {
                    anchor = tb.Text.LastIndexOfAny(new char[] { '\r' }, anchor);
                    if (anchor == -1)
                    {
                        anchor = 0;
                        break;
                    }
                }

                //Select Current Line
                n = 0;
            }

            repeat = n   1;
            for (int i = 0; i < repeat; i  )
            {
                linepos.index0 = tb.Text.LastIndexOfAny(new char[] { '\n' }, anchor);
                if (linepos.index0 == -1)
                {
                    linepos.index0 = 0;
                    break;
                }
                else
                {
                    linepos.index0  ;
                }
            }

            linepos.index1 = tb.Text.IndexOfAny(new char[] { '\n' }, anchor);
            if (linepos.index1 == -1)
                linepos.index1 = linepos.indexz;

            // Length of Line
            linepos.len = linepos.index1 - linepos.index0   1;
            linepos.str = tb.Text.Substring(linepos.index0, linepos.len);
            return linepos;
        }

  • Related