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
- [C#] Check Scrollbars Visibility – find out which scrollbars are visible
- [C#] InputBox – simple static method to show InputBox in C#
- [C#] Hourglass Wait Cursor – how to change mouse cursor to hourglass
- TextBox.SelecÂtionStart – MSDN – beginning of the current selection
- TextBoxBase.ScroÂllToCaret – MSDN – scrolls to the current caret position
- ListBox.SelecÂtedIndex – MSDN – index of the currently selected item
- ListView.EnsuÂreVisible – MSDN – ensures item to be visible, scrolls if necessary
- TreeNode.EnsuÂreVisible – MSDN – ensures node to be visible, expands nodes or scrolls if necessary
- DataGridView.FirÂstDisplayedCeÂll – MSDN – first cell currently displayed in the grid
