Text Box Scroll

SIMIN

Regular
Joined
Mar 10, 2008
Messages
92
Hello,
It seems to be strange, but in a loop, when I add some text to the text box like this:

For X = 1 to 100
TextBox1.Text = TextBox1.Text + "Text" + vbNewLine
Next

However, the scroll bar will stay at top and I can only see the first few lines of text box, even if I move the scroll bar to down, it will jump back to top as soon as text inside the text box changes.
I want to change this scenario, so scroll bar will stay at down so I can see the last lines of text inside text box.
I don't know how can this be done, but I know it's possible because I've seen some applications like this! :confused:
Thank you.
 
SelectionStart

A quick-and-dirty implementation might looks like this:

Visual Basic:
For X = 1 to 100
    TextBox1.Text += "Text" + ControlChars.NewLine
    TextBox1.SelectionStart = TextBox1.Text.Length
Next

Good luck! :cool:
 
Paul's code didn't work for me. But this does:

Code:
    Private Const WM_VSCROLL As Int32 = &H115
    Private Const SB_BOTTOM As Int32 = 7

    Private Declare Auto Function SendMessage Lib "user32.dll" ( _
        ByVal hwnd As IntPtr, _
        ByVal wMsg As Int32, _
        ByVal wParam As Int32, _
        ByVal lParam As Int32 _
    ) As Int32

    Private Sub AddLine(ByVal loDestination As RichTextBox, ByVal lsText As String)
        With loDestination
            .AppendText(lsText & ControlChars.NewLine)
            SendMessage(loDestination.Handle, WM_VSCROLL, SB_BOTTOM, 0)
        End With
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim i As Integer = 0

        For i = 0 To 300
            AddLine(txtProgress, "Line " & i)
        Next
    End Sub

Pete
 
Try this.
Code:
For X = 1 to 100
    TextBox1.Text += "Text" + ControlChars.NewLine
    TextBox1.SelectionStart = TextBox1.Text.Length
  [I]  TextBox1.ScrollToCaret()[/I]
Next
 
Try this.
Code:
For X = 1 to 100
    TextBox1.Text += "Text" + ControlChars.NewLine
    TextBox1.SelectionStart = TextBox1.Text.Length
  [I]  TextBox1.ScrollToCaret()[/I]
Next

Nope, doesn't work. The only one I could get to work was the code I posted. Your code does make sense. But it doesn't work in VS 2005 for me. :(


Pete
 
Works fine here:
Visual Basic:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 1 To 100
            TextBox1.Text += "Text" + i.ToString + ControlChars.NewLine
            TextBox1.SelectionStart = TextBox1.Text.Length
            TextBox1.ScrollToCaret()
        Next
End Sub
 
Works fine here:
Visual Basic:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        For i = 1 To 100
            TextBox1.Text += "Text" + i.ToString + ControlChars.NewLine
            TextBox1.SelectionStart = TextBox1.Text.Length
            TextBox1.ScrollToCaret()
        Next
End Sub

Hmm... I wonder why it doesn't work for me. I have a regular textbox, with "Multiline=True", and "Scrollbar=Vertical". Is that what you have? Am I missing something? I am trying it in VS 2005. Is that what you are using?

Thanks,
Pete
 
Hmm... I wonder why it doesn't work for me. I have a regular textbox, with "Multiline=True", and "Scrollbar=Vertical". Is that what you have? Am I missing something? I am trying it in VS 2005. Is that what you are using?

Thanks,
Pete

Yes. :confused:
When I run this code I see the numbers climb up to 100 and the scrollbar stays at the bottom.
 
Back
Top