dcsimg
Results 1 to 5 of 5

Thread: Focus the last line of the multiline text box vb6

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    7

    Focus the last line of the multiline text box vb6

    Dear friends,
    I have a text box with "multiline" and the "vertical scroll bar" enabled. I am populating the row of records in the text box. Everything is ok but the focus is always stayed on top records. I have to scroll down to bottom manually by pulling down the scroll bar to see the latest records arrived. I want to show the latest records on the text box automatically.
    How can I achieve that?

    Thank you all
    Vibrant

  2. #2
    Lively Member
    Join Date
    Apr 2014
    Location
    Munich, Germany
    Posts
    121

    Re: Focus the last line of the multiline text box vb6

    Quote Originally Posted by pmk1969 View Post
    I want to show the latest records on the text box automatically.
    Actually it's not the focus you are talking about but the cursor position.

    Try like this:
    Code:
    Text1.SelStart = Len(Text1.Text)
    HTH Wolfgang

  3. #3
    Frenzied Member wqweto's Avatar
    Join Date
    May 2011
    Posts
    1,491

    Re: Focus the last line of the multiline text box vb6

    Add a SendMessage Text1.hWnd, WM_VSCROLL, SB_BOTTOM, ByVal 0& if this is a RichTextBox or InkEdit to actually scroll the view -- these beasts are more stubborn :-))

    cheers,
    </wqw>

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,057

    Re: Focus the last line of the multiline text box vb6

    If you want the 'latest records' to be displayed, why not put them on TOP (reverse your query to your DB)?

    NORMALLY, I would put the most recent records in a ML textbox, listbox, listview (or whatever) as the first records, not the last. You CAN, however, do as wqw states, but maybe better logic says put the latest record on top, the oldest on the bottom (and of course, all sorted likewise in between).

  5. #5
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    5,663

    Re: Focus the last line of the multiline text box vb6

    Here's a little function I sometimes use for this stuff:

    Code:
    
    Option Explicit
    '
    Private Declare Function SendMessageW Lib "user32" (ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
    '
    
    
    Private Sub Form_Click()
        out(Text1) = "asdf"
        out(Text1, True) = "qwer-"
        out(Text1) = "rewq"
        out(Text1) = "----" & Format$(Rnd) & "----"
    End Sub
    
    
    
    
    
    
    Private Property Let out(txt As VB.TextBox, Optional bHoldLine As Boolean, s As String)
        Const EM_SETSEL = &HB1
        Const EM_REPLACESEL = &HC2
        '
        SendMessageW txt.hWnd, EM_SETSEL, &H7FFFFFFF, ByVal &H7FFFFFFF          ' txt.SelStart = &H7FFFFFFF
        If bHoldLine Then
            SendMessageW txt.hWnd, EM_REPLACESEL, 0, ByVal StrPtr(s)            ' txt.SelText = s
        Else
            SendMessageW txt.hWnd, EM_REPLACESEL, 0, ByVal StrPtr(s)            ' txt.SelText = s
            SendMessageW txt.hWnd, EM_SETSEL, &H7FFFFFFF, ByVal &H7FFFFFFF      ' txt.SelStart = &H7FFFFFFF
            SendMessageW txt.hWnd, EM_REPLACESEL, 0, ByVal StrPtr(vbCrLf)       ' txt.SelText = vbCrLf
        End If
    End Property
    
    
    

    It has the added advantage that you can append text to the end of the textbox MUCH faster than doing a concatenation of the .Text property each time.

    To use, just put the above code into a Form1, throw a Text1 onto that form and set it's MultiLine property to true. Also add a vertical scrollbar to the Text1 if you like.

    It works particularly well if it's a locked (view only) textbox. Someday I should look at some of LaVolpe's work and make it full Unicode for view only.

    And it keeps the carat at the end.

    Enjoy,
    Elroy
    Last edited by Elroy; Aug 5th, 2019 at 09:50 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

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