Re: RichTextBox Display the latest line

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Herfried K. Wagner [MVP] (hirf-spam-me-here_at_gmx.at)
Date: 07/14/04


Date: 15 Jul 2004 01:13:08 +0200


* zhangd@tycoelectronics.com (dale zhang) scripsit:
> I am using a richtextbox to display multiple lines of received data.
> When we receive more lines of data, we could use scrollbar to see
> them. But how could we display the latest data all the time (i mean
> that data will move up automatically if there are more lines to
> display)?

My FAQ:

When using a RichTextBox control for displaying logging information, it is
useful to scroll the recently added line into view. There are two ways to
accomplish this:

\\\
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 Destination As RichTextBox, ByVal Text As String)
    With Destination
        .AppendText(Text & ControlChars.NewLine)
        SendMessage(Destination.Handle, WM_VSCROLL, SB_BOTTOM, 0)
    End With
End Sub
///

- or -

\\\
Dim ctr As Control = Me.ActiveControl
With Me.RichTextBox1
    .Focus()
    .AppendText("Hello World!")
    .ScrollToCaret()
End With
ctr.Focus()
///

The 2nd solution has a side-effect: Setting the focus to the
RichTextBox control may raise validation events.

-- 
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>


Relevant Pages

  • Re: In der RichTextBox zum Ende scrollen
    ... When using a RichTextBox control for displaying logging information, ... useful to scroll the recently added line into view. ... Private Const WM_VSCROLL As Int32 = &H115 ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: How to display the latest line of data in RichTextBox in VS.NET?
    ... I have a question about the receiver in the ... When using a RichTextBox control for displaying logging information, ... Private Const WM_VSCROLL As Int32 = &H115 ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: TextBox: autoscrolling to end after AppendText
    ... Herfried Wagner has your answer in the microsoft.public.dotnet.language.vb ... When using a RichTextBox control for displaying logging information, ... Private Const WM_VSCROLL As Int32 = &H115 ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: RichTextBox Error with AppendText()
    ... Check (RichTextBox.InvokeRequired) - if this is true, call RichTextBox.Invoke or RichTextBox.BeginInvoke on an EventHandler to update the RichTextBox. ... > IntPtr buf, Int32 cb, Int32& transferred) ... > wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) ... > at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: RTF-Control
    ... When using a RichTextBox control for displaying logging information, ... Private Const WM_VSCROLL As Int32 = &H115 ... ByVal hwnd As IntPtr, _ ... Private Sub AddLine(ByVal Destination As RichTextBox, ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)