dcsimg
Results 1 to 11 of 11

Thread: move cursor to beginning of line in textbox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    6

    move cursor to beginning of line in textbox

    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!
    Code:
    '******************************************
    ' 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.
        Do
           i = InStr(Data, Chr$(8))
           If i Then
              If i = 1 Then
                Text1.SelStart = TermSize - 1
                Text1.SelLength = 1
                 Data = Mid$(Data, i + 1)
              Else
               Data = Left$(Data, i - 2) & Mid$(Data, i + 1)
              End If
           End If
        Loop While i
      ' Eliminate line feeds.
        Do
           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
        Do
           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
    
    Handler:
        MsgBox Error$
        Resume Next
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: move cursor to beginning of line in textbox

    You can send the EM_LINEFROMCHAR (to get the current line #) and EM_LINEINDEX (to get the character index of the start of the current line) messages to the TextBox to accomplish your goal.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    6

    Re: move cursor to beginning of line in textbox

    Quote Originally Posted by jpbro View Post
    You can send the EM_LINEFROMCHAR (to get the current line #) and EM_LINEINDEX (to get the character index of the start of the current line) messages to the TextBox to accomplish your goal.
    are you sure those links are VB6?
    I am lost.

    Any examples?

  4. #4
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: move cursor to beginning of line in textbox

    They're messages that can be send to the underlying Win32 Edit window of the VB6 TextBox control. You will need to use the SendMessage API (there should be lots of examples around if you search for "SendMessage em_lineindex em_linefromchar VB6" in Google). Here's one resource I found that should clear things up a bit: http://vbnet.mvps.org/code/textapi/txapiselect.htm

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    6

    Re: move cursor to beginning of line in textbox

    This is kind of WAY over my head!

    I made an exe out of the code from the link, and it works.

    However, it only selects the line, it does not move the cursor to the start.

    I just want to move the cursor to the beginning of the line so the next data will overwrite the current data.

  6. #6
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: move cursor to beginning of line in textbox

    The code at the link was just to give you a nudge in the right direction, it's not the exact solution you are looking for.

    I had a look at the code at the link I provided, and it looks like the Command3_Click event gets you pretty close:

    Code:
    Private Sub Command3_Click()
    
       'Typically this would be called from
       'a menu item, for example 'Select To Beginning of Line'
        Dim cursorPos As Long
        Dim currLine As Long
        Dim chrsToCurrent As Long
        On Local Error Resume Next
       
       'get the cursor position in the textbox
        Call SendMessage(Text1.hwnd, _
                         EM_GETSEL, 0, cursorPos)
       
       'get the current line index
        currLine = SendMessage(Text1.hwnd, _
                               EM_LINEFROMCHAR, _
                               cursorPos, _
                               ByVal 0&) ' + 1
       
       'number of chrs up to the current line
        chrsToCurrent = SendMessage(Text1.hwnd, _
                                    EM_LINEINDEX, _
                                    currLine, ByVal 0&)
    
       'select from the first chr on the
       'cursor line up to the cursor
        Text1.SetFocus
        Call SendMessage(Text1.hwnd, _
                         EM_SETSEL, _
                         chrsToCurrent, _
                         ByVal cursorPos)
    
    End Sub
    Note the comment "Get the cursor position of the TextBox" - you can skip this part and use the TextBox.SelStart property instead.

    Next comment is "get the current line index" - this is a line of code you'll need because we need to figure out what line # the cursor is on. So you can do something like:

    Code:
       'get the current line index
        currLine = SendMessage(Text1.hwnd, _
                               EM_LINEFROMCHAR, _
                               TextBox1.SelStart, _
                               ByVal 0&)
    To get the line index of the SelStart of a control called TextBox1.

    The next comment is "number of chrs up to the current line" which is another way of saying get the starting character of the current line. So you can use this code verbatim to get the position you want to move the caret to:

    Code:
       'number of chrs up to the current line
        chrsToCurrent = SendMessage(Text1.hwnd, _
                                    EM_LINEINDEX, _
                                    currLine, ByVal 0&)
    Finally, you have to actually move the caret to the current line start index. You can do this as follows:

    Code:
    TextBox1.SelStart = chrsToCurrent
    I haven't actually tested the above, but it should work.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    6

    Re: move cursor to beginning of line in textbox

    This is kind of WAY over my head!

    I made an exe out of the code from the link, and it works.

    However, it only selects the line, it does not move the cursor to the start.

    I just want to move the cursor to the beginning of the line so the next data will overwrite the current data.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    6

    Re: move cursor to beginning of line in textbox

    thanks, I did figure out that it was command 3. I will have a look at the above and let you know how it goes.

    I appreciate your help!

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    6

    Re: move cursor to beginning of line in textbox

    I really appreciate your help, thanks again!

    Well I got it to work, but moving it over to my application is more difficult than I expected.

    When a carriage return comes in to the text box (Chr 13), the cursor should go to the start of the current line.

    When both a carriage return and a line feed comes in to the text box(Chr 13) & (Chr10), the cursor should go to the first position on the next line.

    This is what I have so far:
    Code:
    '*******************************************************
    ' 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
        Dim cursorPos As Long
        Dim currLine As Long
        Dim chrsToCurrent As Long
        On Local Error Resume Next
    '
        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.
        Do
           i = InStr(Data, Chr$(8))
           If i Then
              If i = 1 Then
                Text1.SelStart = TermSize - 1
                Text1.SelLength = 1
                 Data = Mid$(Data, i + 1)
              Else
               Data = Left$(Data, i - 2) & Mid$(Data, i + 1)
              End If
           End If
        Loop While i
        '
            'Check for beep char, don't print beep char!
           i = InStr(Data, Chr$(7))    '7 = beep
           If i Then
       '    Beep
                Text1.SelStart = TermSize
                Text1.SelLength = 1
                 Data = Mid$(Data, i + 1)
           End If
        '
     	i = 1
            i = InStr(i, Data, Chr$(13))
           If i Then
            'get the current line index
        currLine = SendMessage(Text1.hwnd, _
                               EM_LINEFROMCHAR, _
                               Text1.SelStart, _
                               ByVal 0&)
    
    'number of chrs up to the current line
        chrsToCurrent = SendMessage(Text1.hwnd, _
                                    EM_LINEINDEX, _
                                    currLine, ByVal 0&)
           End If
    '
        Text1.SelText = Data 'put the data in the text box
    
    ' move the cursor to the current line start index.
    
    Text1.SelStart = chrsToCurrent
        Text1.SetFocus
    Exit Sub
    
    Handler:
        MsgBox Error$
        Resume Next
    End Sub

  10. #10
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: move cursor to beginning of line in textbox

    I don't see any handling of character code 10 in your code. You'll need to test for Chr$(10) and then you can bump down to the beginning of the next line using EM_LINEFROMCHAR with Text1.SelStart, then send EM_LINEINDEX with the result of EM_LINEFORCHAR message call + 1 (for the line after the current line), and finally set SelStart to the result of your EM_LINEINDEX call.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,546

    Re: move cursor to beginning of line in textbox

    Seems like you need to update your header comment as well, since it seems like you might have a logic conflict with how you treat linefeeds since you say the code should add linefeeds when you receive a carriage return.
    Code:
    ' Line feed characters are appended to all carriage
    ' returns.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width