|
Question : I want the richtextbox scroll stick to the buttom instead of the top. How?
|
|
Hi,
I want the richtextbox scroll stick to the buttom instead of the top. How?
Because now I added a richtextbox, I append text into it, he doesnt scroll down, I want it to scroll down to the last line.
thx
|
|
Answer : I want the richtextbox scroll stick to the buttom instead of the top. How?
|
|
Here is project I whipped up demonstrating an RTB with a "scroll lock" as you describe. Just type into the two TextBoxes at the top to simulate a "chat room". If the ScrollBar is at the bottom of the RTB, then the RTB will scroll to the newly appended text. Otherwise, it simply stays where it is...
Public Class Form2 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents Timer1 As System.Windows.Forms.Timer erStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.Timer1 = New System.Windows.Forms.Timer(Me.components) Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(8, 8) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(256, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" ' 'TextBox2 ' Me.TextBox2.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.TextBox2.Location = New System.Drawing.Point(272, 8) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(256, 20) Me.TextBox2.TabIndex = 1 Me.TextBox2.Text = "" ' 'RichTextBox1 ' Me.RichTextBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.RichTextBox1.Location = New System.Drawing.Point(8, 32) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.ReadOnly = True Me.RichTextBox1.Size = New System.Drawing.Size(520, 208) Me.RichTextBox1.TabIndex = 2 Me.RichTextBox1.TabStop = False Me.RichTextBox1.Text = "" ' 'Timer1 ' Me.Timer1.Enabled = True ' 'Form2 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(536, 246) Me.Controls.Add(Me.RichTextBox1) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.TextBox1) Me.Name = "Form2" Me.Text = "Chat Simulator" Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load sci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(sci) sci.fMask = SIF_ALL End Sub
Private Structure SCROLLINFO Public cbSize As Integer Public fMask As Integer Public nMin As Integer Public nMax As Integer Public nPage As Integer Public nPos As Integer Public nTrackPos As Integer End Structure
Private Declare Function GetScrollInfo Lib "user32" _ (ByVal hwnd As Integer, ByVal n As Integer, _ ByRef lpScrollInfo As SCROLLINFO) As Integer
Private Const WM_VSCROLL As Integer = &H115 Private Const WM_HSCROLL As Integer = &H114
Private Const SB_VERT As Integer = 1 Private Const SB_HORZ As Integer = 0 Private Const SIF_PAGE As Integer = &H2 Private Const SIF_POS As Integer = &H4 Private Const SIF_RANGE As Integer = &H1 Private Const SIF_TRACKPOS As Integer = &H10 Private Const SIF_ALL As Integer = (SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS)
Private sci As SCROLLINFO
Private Sub Form2_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged Dim padding As Integer = TextBox1.Left * 3 Dim tbWidth As Integer = (Me.ClientSize.Width - padding) / 2 TextBox1.Width = tbWidth TextBox2.Width = tbWidth TextBox2.Left = (Me.ClientSize.Width / 2) + (TextBox1.Left / 2) End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then e.Handled = True ' suppress the beep AddText(RichTextBox1, TextBox1.Text, Color.Blue) TextBox1.Text = "" TextBox1.Focus() End If End Sub
Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then e.Handled = True ' suppress the beep AddText(RichTextBox1, TextBox2.Text, Color.Red) TextBox2.Text = "" TextBox2.Focus() End If End Sub
Private Sub AddText(ByVal RTB As RichTextBox, ByVal Text As String, ByVal C As Color) Dim vp As Byte = VerticalScrolledPercentage(RTB) Dim TimeTag As String = "[" & DateTime.Now.ToShortTimeString & "] " RTB.SelectionStart = RTB.TextLength RTB.SelectionColor = Color.Black RTB.AppendText(TimeTag) RTB.SelectionColor = C RTB.AppendText(Text & vbCrLf) If vp = 100 Then RTB.Focus() RTB.ScrollToCaret() End If End Sub
Private Function HorizontalScrolledPercentage(ByVal RTB As RichTextBox) As Byte If GetScrollInfo(Me.Handle.ToInt32, SB_HORZ, sci) <> 0 Then Return CInt(sci.nPos / (sci.nMax - sci.nPage) * 100) End If End Function
Private Function VerticalScrolledPercentage(ByVal RTB As RichTextBox) As Byte If GetScrollInfo(RTB.Handle.ToInt32, SB_VERT, sci) <> 0 Then Return CInt(sci.nPos / (sci.nMax - sci.nPage) * 100) End If End Function
End Class
|
|
|
|