RTF Location No2 - VB2005

Hi,

I asked this Q a while ago and got a response. The question was that how to get the Column and Line location for a text editor. This code works perfectly, but it shows line and column 0,0. I would like it to start on Line, Column position no. 1,1.

Dim Ln As Integer = Me.RichTextBox1.GetLineFromCharIndex(Me.RichTextBox1.SelectionStart)

Dim Cn As Integer = (Me.RichTextBox1.SelectionStart) - (Me.RichTextBox1.GetFirstCharIndexFromLine(Ln))

Dim MyString As String = "Line: " & Ln.ToString & " Col: " & Cn.ToString

Me.ToolStripStatusLabel1.Text = (MyString)

I have tried adding a 1 or specifying other integers to 1 but cannot see to get it to work(probably due to some stupid sting i am doing)

Can anyone please help

Thanks




Answer this question

RTF Location No2 - VB2005

  • Bugsflowers

    Hi,

    I found the problem!!!

    I used a command on the textbox, on the keypress function.

    Deleted that and it works.

    Thanks for the help.



  • ptjhuang

    By doing this it sets the zoro line or column to 1 then back to zero.

    Have no idea why this is. It makes sense by adding a 1, but i think that it not there that you shoul ad it. Maybe sooner.

    I tried everything, just cannot get it to work!

    Maybe my fault...



  • Siri Vellanki - MSFT

    The arrays are 0 based so as you say yourself you need to add 1. But it can be sensitive where you do it. My additions in bold

    Dim Ln As Integer = Me.RichTextBox1.GetLineFromCharIndex(Me.RichTextBox1.SelectionStart)
    Dim Cn As Integer = (Me.RichTextBox1.SelectionStart) - (Me.RichTextBox1.GetFirstCharIndexFromLine(Ln)) + 1
    Ln = Ln + 1
    Dim MyString As String = "Line: " & Ln.ToString & " Col: " & Cn.ToString



  • GavinRitchie

    It works well for me. I created a form with 2 control on it. One RichTextBox named RichTextBox1 and one TextBox named TextBox1

    Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
       Dim nPos As Integer = Me.RichTextBox1.SelectionStart + Me
    .RichTextBox1.SelectionLength
       Dim Ln As Integer = Me
    .RichTextBox1.GetLineFromCharIndex(nPos)
       Dim Cn As Integer = nPos - (Me
    .RichTextBox1.GetFirstCharIndexFromLine(Ln)) + 1
       Ln = Ln + 1
       TextBox1.Text = Ln.ToString &
    ":"
    & Cn.ToString
    End Sub

    The textbox gets the LINE:COL numbers I would expect.

    Note that I get the position as start+length. This is because when you select text the cursor is at the end of the selection.



  • RTF Location No2 - VB2005