C# Examples

Menu:


Autoscroll (TextBox, ListBox, ListView) [C#]

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

[C#]
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

ListBox autoscroll

[C#]
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;

ListView autoscroll

[C#]
listView1.EnsureVisible(listView1.Items.Count - 1);

TreeView autoscroll

[C#]
treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();

DataGridView autoscroll

[C#]
dataGridView1.FirstDisplayedCell =
  dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];


See also

By Franta Zahora, 24-Jul-2009