Jul 1, 2012

Auto Scroll in Common Controls

Leave a Comment
This example shows, 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 recently added item is visible without the necessary to do it manually.

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

TextBox autoscroll


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

ListBox autoscroll


Code:
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;

ListView autoscroll


Code:
listView1.EnsureVisible(listView1.Items.Count - 1);

TreeView autoscroll


Code:
treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();

DataGridView autoscroll


Code:
dataGridView1.FirstDisplayedCell =
  dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];


If you feel this is helpful or you like it, Please share this using share buttons available on page.

0 comments :

Post a Comment