Hi everyone,

I am trying to move the cursor to the beginning of the current line in a textbox.
This is a basic terminal program. Data comes in from the serial port and is displayed in the textbox.

When the program receives a 13 (carriage return) from the serial port, it should bring the cursor to the beginning of the same line. All I have managed to do is bring the cursor to the beginning of the following line.

Can anyone help me please?
Any help is greatly appreciated!
' This procedure adds data to the Term control's Text property.
' It also filters control characters, such as BACKSPACE,
' carriage return, and line feeds, and writes data to
' an open log file.
' BACKSPACE characters delete the character to the left,
' either in the Text property, or the passed string.
' Line feed characters are appended to all carriage
' returns.  The size of the Term control's Text
' property is also monitored so that it never
' exceeds MAXTERMSIZE characters.
Private Static Sub ShowData(Text1 As Control, Data As String)
    Dim TermSize As Long, i
    Dim lCurPos As Long, lLineLength
    TermSize = Len(Text1.Text)
' Point to the end of Term's data.
       Text1.SelStart = TermSize
  'Determine Cursor Position
'         If Text1.SelLength = 0 Then
'            lCurPos = Text1.SelStart
'         Else
'            lCurPos = Text1.SelStart + Text1.SelLength
'         End If
'     Determine the Line Length
'     lLineLength = SendMessage(Text1.hwnd, EM_LINELENGTH, lCurPos, 0)

' Filter/handle BACKSPACE characters.
       i = InStr(Data, Chr$(8))
       If i Then
          If i = 1 Then
            Text1.SelStart = TermSize - 1
            Text1.SelLength = 1
             Data = Mid$(Data, i + 1)
           Data = Left$(Data, i - 2) & Mid$(Data, i + 1)
          End If
       End If
    Loop While i
  ' Eliminate line feeds.
       i = InStr(Data, Chr$(10))
       If i Then
         Data = Left$(Data, i - 1) & Mid$(Data, i + 1)
       End If
    Loop While i
' Make sure all carriage returns have a line feed.
'this is where I am stuck
'I don't want to add a line Feed (Chr$(10))
'I just want to position the curor at the beginning of the current line
    i = 1
       i = InStr(i, Data, Chr$(13))
       If i Then
   '       Data = Left$(Data, i) & Chr$(10) & Mid$(Data, i + 1)
          Data = Left$(Data, i) & Mid$(Data, i + 1)
          i = i + 1
       End If
    Loop While i
    Text1.SelText = Data
    Text1.SelStart = Len(Text1.Text)
Exit Sub

    MsgBox Error$
    Resume Next
End Sub