Get current Line and char offsett in vs.net

createdbyx

Freshman
Joined
Oct 30, 2003
Messages
34
I need to know how get retrieve the current Line and
character offset for the active code document in visual
studio.net

for example if i wanted to keep track of exactly what
point the caret is at inside a document. Basically I want
to keep track of every character the user typins into the
code document as they type it.

Here is some ~example~ code to give you an idea of what I
am trying to do.

Code:
       Private Sub mobjTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles mobjTimer.Tick
        Dim NewPoint As TextPoint
        Dim TD As TextDocument
        Dim TS As TextSelection

        ' stop timer
        mobjTimer.Stop()

        Try
            ' get current code document
            TD = DirectCast(mobjHelper.DTE.ActiveDocument.Object("TextDocument"), TextDocument)
        Catch
            'ERR: do nothing?
        End Try

        If Not TD Is Nothing Then
            TS = TD.Selection
            If PrevPos Is Nothing Then PrevPos = NewPoint

            'check if position of chret changed
            If PrevPos.Line = NewPoint.Line Then
                If NewPoint.LineCharOffset <> PrevPos.LineCharOffset Then
                    AddTaskListItem("line pos changed")
                    AddTaskListItem(TD.CreateEditPoint(PrevPos).GetText(NewPoint))

                    mstrCodeTyped.Append(TD.CreateEditPoint(PrevPos).GetText(NewPoint))
                End If
            End If

            PrevPos = NewPoint
        End If

        mobjTimer.Start()
    End Sub
 
Back
Top