locked
TextBox. GetLineIndexFromCharacterIndex returns -1 when passing in large text RRS feed

  • Question

  • The sample code below calls the GetLineIndexFromCharacterIndex method on the TextBox and it returns -1 when passing in text 55,000 or larger.  Is this a bug and is there a work-around for this?

    public MainWindow()
    {
        InitializeComponent();
        string text = "";
        string path = "problematicFile.txt";
    
        // This text is added only once to the file.
        if (File.Exists(path))
        { 
            text = System.IO.File.ReadAllText(path);
        }            
        textBox.Text = text;
        var chararcter1 = textBox.Text[56218];
        var chararcter2 = textBox.Text[56325];
        int index1 = textBox.GetLineIndexFromCharacterIndex(56218);
        int index2 = textBox.GetLineIndexFromCharacterIndex(56325);
    }
    


    Thank You!

    Sunday, March 22, 2020 10:21 PM

All replies

  • There may be, have not tried stuffing that many lines into a TextBox, you could try the following extension method, place into a static class in your project to see if that works.

    Taken from https://stackoverflow.com/questions/15204830/get-index-of-line-from-textbox-c-sharp

    public static int GetLineIndex(this TextBox textbox, int line)
    {
        var text = textbox.Text;
        var thisLine = 0;
        for (var i = 0; i < text.Length; i++)
        {
            if (thisLine == line)
                return i;
    
            if (text[i] == '\n')
                ++thisLine;
        }
    
        throw new ArgumentOutOfRangeException();
    }

    Or report this to Microsoft via the feedback button in Visual Studio. 

    Since this is WPF your post is being moved to the WPF forum.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 22, 2020 11:02 PM
  • Thank you but its better to use the built-in method if possible.  However, we found that its recorded as a bug back in 2011.  However, at the bottom of the page is a link to Connect which is out of service.

    TextBox: problems with line indices

    I guess MS is tired of fixing bug?

    Does anyone have more info on a fix for this?

    Thank you.


    • Edited by moondaddy Monday, March 23, 2020 6:33 PM forgot
    Monday, March 23, 2020 6:32 PM
  • Hi,

    Since MSDN WPF forum will be read only from 3/30/2020, we have created a new thread on Microsoft Q&A TextBox. GetLineIndexFromCharacterIndex returns -1 when passing in large text, please follow this thread for further discussion about your question.

    Best Regards,
    Charles


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 27, 2020 8:35 AM
  • Why will "MSDN WPF forum will be read only"?   Is it going the way of Silverlight?
    Friday, March 27, 2020 3:19 PM