title: autoscroll to bottom of multiline textbox being updated by backgroundworker
body: <p>I have a background worker control that is set to perform a task, and update a multiline text box on my main UI using a delegate procedure. this is all working perfectly, however once the updating scrolls off the bottom of the text box, the scroll bars appear, but the continuous refreshing causes the text box to stay locked at the top. Ideally, I would like the text box to auto-scroll itself to the bottom to show the latest entry in real-time. What would be the best way to implement this?</p>
  
  <p>I have tried using the <code>scrolltocaret()</code> method, with and without a <code>SelectionStart = txtlog.Text.Length</code> command preceding it. perhaps I'm putting it in the wrong place?</p>
  
  <p>some code samples below:</p>
  
  <p>Delegate code:</p>
  
  <pre><code>Delegate Sub updateresults_delegate(ByVal textbox As TextBox, ByVal text As String)
  
  Private Sub updatelog_threadsafe(ByVal textbox As TextBox, ByVal text As String)
              If textbox.InvokeRequired Then
                  Dim mydelegate As New updateresults_delegate(AddressOf updatelog_threadsafe)
                  Me.Invoke(mydelegate, New Object() {textbox, text})
                  'Me.txtlog.SelectionStart = txtlog.Text.Length
                  'Me.txtlog.ScrollToCaret()
              Else
                  textbox.Text = text
              End If
          End Sub
  </code></pre>
  
  <p>main backgroundworker activity:</p>
  
  <pre><code>For i As Integer = val1 To val2
  'generate an IP address from split host parts and current value of i
                  host = s1(0) &amp; "." &amp; s1(1) &amp; "." &amp; s1(2) &amp; "." &amp; i
                  Try 'attempt to ping the IP
                      Dim reply As PingReply = pingsender.Send(host, timeoutval, buffer, options)
                      If reply.Status = IPStatus.Success Then
                          name = System.Net.Dns.GetHostEntry(host)'get DNS entry
                          resulttext += String.Format("{1} - {2}: reply: Bytes={3} time{4} TTL={5}{0}", vbCrLf, name.HostName, reply.Address.ToString, reply.Buffer.Length, getms(reply.RoundtripTime), reply.Options.Ttl) 'print out success text
                      Else
                          resulttext += String.Format("      {1}: Ping failed. {2}{0}", vbCrLf, host, reply.Status.ToString) 'print out fail text
                      End If
                      updatelog_threadsafe(txtlog, resulttext) 'send text to textbox
  
              System.Threading.Thread.Sleep(1000)
          Catch ex As Exception
  
          End Try
      Next
  </code></pre>
  
  <p>I guess my main question is: I'm pretty certain that the <code>textbox.scrolltocaret()</code> is the correct method to use for what I want, but where is the best place for me to put it? I've tried it in the delegate, the main backgroundworker, as well as before &amp; after the <code>runworkerasync()</code> method. none of these worked, and now I'm stumped!</p>