Autoscroll WinForm controls TextBox, ListBox, ListView, TreeView and DataGridView.

Prabhukiran345
Posted by Prabhukiran345 under C# category on | Points: 40 | Views : 4681
This example demonstrates how to programatically autoscroll WinForm controls TextBox, ListBox, ListView, TreeView and DataGridView

Autoscroll to the end of box or view is useful, for example, in situations when you use one of these components as the output log window. Usually you add the items to the box or view and you want to be sure that the last added item is visible without the necessary to do it manually.

Though it's not difficult to solve this problem, .NET doesn't provide any unified way how to do it.

TextBox autoscroll

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

ListBox autoscroll
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;

ListView autoscroll
listView1.EnsureVisible(listView1.Items.Count - 1);

TreeView autoscroll
treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();

DataGridView autoscroll
dataGridView1.FirstDisplayedCell =
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];

Comments or Responses

Login to post response