How can I display the line position in a TextBox on the status bar?

送分小仙女□ 提交于 2019-12-24 16:43:09

问题


I have added a StatusStrip control and placed a StatusLabel inside of it. But now I want to know how to connect it to my TextBox to show the line number and position of the cursor, like: "Line 2, Row 6".

Thank you


回答1:


  1. Get the index of the caret in the TextBox:

    C#

    int caretIndex = textBox.SelectionStart;
    

    VB.NET

    Dim caretIndex As Integer = textBox.SelectionStart
    
  2. Get the line number from the caret index:

    C#

    int lineNumber = textBox.GetLineFromCharIndex(caretIndex);
    

    VB.NET

    Dim lineNumber As Integer = textBox.GetLineFromCharIndex(caretIndex)
    
  3. Get the character index in the current line:

    C#

    Point characterXY = textBox.GetPositionFromCharIndex(caretIndex);
    int characterIndex = textBox.GetCharIndexFromPosition(characterXY);
    

    VB.NET

    Dim characterXY As Point = textBox.GetPositionFromCharIndex(caretIndex)
    Dim characterIndex As Integer = textBox.GetCharIndexFromPosition(characterXY)
    

I guess you can continue from here ...



来源:https://stackoverflow.com/questions/5692714/how-can-i-display-the-line-position-in-a-textbox-on-the-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!