TextBox Control

How to constantly scroll to the end of text in multiline text box

A TextBox control is used to display, or accept as input, a single line of text. This control has additional functionality that is not found in the standard Windows text box control, including multiline editing and password character masking.

Multiline Text Box

A multiline text box allows you to display more than one line of text in the control. The Multiline Property will gets or sets a value indicating whether this is a multiline TextBox control.

// Set the Multiline property to true.
textBox1.Multiline = true;

Scroll Bars in Multiline Text Box

autoscroll to bottom of multiline textbox

You can add scroll bars to a text box using the ScrollBars property to display horizontal and/or vertical scroll bars. This allows the user to scroll through the text that extends beyond the dimensions of the control.

// Add vertical scroll bars to the TextBox control.
textBox1.ScrollBars = ScrollBars.Vertical;

If you want both scroll bars(vertical and horizondal) you should set ..

textBox2.ScrollBars = ScrollBars.Both;

Automatically scroll to the bottom of a multiline text box

If you use TextBox.AppendText(string text), it will automatically scroll to the end of the newly appended text. It avoids the flickering scrollbar if you are calling it in a loop.

textBox2.AppendText(textBox1.Text);

Internally, AppendText does something like this:

textBox2.Select(textBox.TextLength + 1, 0);
textBox2.SelectedText = textToAppend;

C# Source Code

using System;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.AppendText(textBox1.Text);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            textBox2.Text = "Your text here !!";
        }
    }
}

VB.Net Source Code

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.AppendText(TextBox1.Text)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox2.Text = "Your text here"
    End Sub
End Class

You can perform another way to achieve scrollbar at the end of the multiline textbox to auto scroll on the TextChanged event:

C#

textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Refresh();

VB.Net

textBox1.SelectionStart = textBox1.Text.Length
textBox1.ScrollToCaret()
textBox1.Refresh()


NEXT.....Convert byte array to hex string