CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 35
  1. #1
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Scrolling to textbox bottom without focus ?

    Hey,

    I have a textbox control that functions as a log window for my application, and I've successfully made it scroll to the bottom automatically.

    Unfortunately the function used gives the window focus (While I don't want that) I've tried using selStart with the textbox length, and I tried using the following API call:

    Code:
    SendMessage txtLog.hWnd, WMVSCROLL, SBBOTTOM, 0
    Is there any other API call or anything that makes it scroll to the bottom without giving focus to it ?

    Edit: Just changing the text of the textbox gives it focus apperently, is there any way to prevent this ?

    Thanks for your time, Xeross

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Scrolling to textbox bottom without focus ?

    Use this:
    Code:
      txtLog.Selstart = Len(txtLog.Text)
    If you add vbCrLf to your log messages already, then you might wish to subtract 2 from the length.
    The focus is obviously not given to the textbox as far as I have tried.

  3. #3
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    It seems just adding text to it gives it focus somehow.

  4. #4
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    Code:
        With frm_log.txtLog
            If .Text <> "" Then
                .Text = .Text & vbNewLine
            End If
            .Text = .Text & "[" & cDay & "/" & UCase(MonthName(Month(Now), True)) & "/" & cYear & "][" & cHour & ":" & cMinute & ":" & cSecond & "] "
            .Text = .Text & outStr
            Call SendMessage(.hWnd, WMVSCROLL, SBBOTTOM, 0)
        End With
    Note it also gets focus without the sendMessage.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Scrolling to textbox bottom without focus ?

    I tried this in a form with a TextBox and a button:
    Code:
    Private Sub Form_Activate()
      Command1.SetFocus
      Text1.Text = "huhu"
    
    End Sub
    Putting text into the box does NOT set the focus to it. It must happen earlier.

  6. #6
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    I found it now, I had a call in the log script that did frm_log.Show() which triggered it to get focus, I just added a check now if it's already visible and that fixed it.

    The only problem remaining is that it tends to scroll back up to the top of the textbox and then back down when I add text (This happens in a very short timeperiod most of the time it's unnoticable) any way around this ?

    Regards, Xeross

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Scrolling to textbox bottom without focus ?

    I use a texbox for logging in quite a similar way.
    Code:
    txtlog.text = textlog.text + newtext$ + vbcrlf
    txtLog.Selstart = Len(txtLog.Text)
    It never scrolled up by itself. Keep the SelStart always at the end of the text. The SendMessage you are using does not influence the SelStart which is the actual cursor position. If this stays somewhere at the top it could be that it tends to scroll up again.

  8. #8
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    I'm using the selStart again, but somehow I still get these jumps to the top though they are almost unnoticable.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Scrolling to textbox bottom without focus ?

    Strange... you could try finding out if other unwanted events occur, liek keydown, keypress, getfocus, by setting a msgbox instruction into the event handlers.

    Does the jump occur, when you add the new text?

  10. #10
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    It adds when adding new lines, replacing lines, or adding something to the current line.

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Scrolling to textbox bottom without focus ?

    Okay. Another method of adding text to the textbox is inserting it at the cursorposition.
    Code:
    txtlog.Selstart=len(txtlog.text)
    txtlog.SelText=NewText
    You could try this instead of txtlog.text = txtlog.text + NewText

  12. #12
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    That might work.

    And I think I just figured out why it would flash back up and then down: When you set the contents the thing gets emptied (Causing the scroll-up) and then it gets filled again.

    I'll test it tomorrow. thanks.

  13. #13
    Join Date
    Apr 2009
    Posts
    394

    Re: Scrolling to textbox bottom without focus ?

    Wof! Bad Wof! using + to concatinate strings. Bad Wof!

  14. #14
    Join Date
    Oct 2008
    Location
    The Netherlands
    Posts
    18

    Re: Scrolling to textbox bottom without focus ?

    Didn't even notice that

  15. #15
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Scrolling to textbox bottom without focus ?

    Shame, shame on me. There are always these blanks I have to type when using the &.

    And you know, these days I completely neglect to Set my objevt variables to Nothing, at end of sub, even at Form_Unload(), too.
    I hope I find forgiveness, when once in VB heaven.

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured