May 11, 2009

Scrolling in Multiline TextBox

In windows forms, We can have feature of scrolling for Multiline TextBox.

We can find text position to scroll up to. One can also scroll to the end of text box by setting the SelectionStart property appropriately.

Following example shows, how to scroll to a specific word in the document. Actually you can use this function for providing search functionality for TextBox.

 public partial class Form1 : Form
        public Form1()
        int ind = 0;
        int be = 0;
        private void button1_Click(object sender, EventArgs e)
            be = textBox1.Text.ToLower().IndexOf("as", ind + 1);
            if (be >= 0)
                textBox1.SelectionStart = be;
                textBox1.Select(be, 2);
                ind = textBox1.Text.ToLower().IndexOf("as", be);
        private void Form1_Load(object sender, EventArgs e)
            textBox1.Text = "In 1924, Einstein received a description of a statistical model from Indian physicist Satyendra Nath Bose, based on a counting method that assumed that light could be understood as a gas of indistinguishable particles. Bose's statistics applied to some atoms as well as to the proposed light particles, and Einstein submitted his translation of Bose's paper to the Zeitschrift für Physik. Einstein also published his own articles describing the model and its implications, among them the Bose-Einstein condensate phenomenon that should appear at very low temperatures . It was not until 1995 that the first such condensate was produced experimentally by Eric Allin Cornell and Carl Wieman using ultra-cooling equipment built at the NIST-JILA laboratory at the University of Colorado at Boulder. Bose-Einstein statistics are now used to describe the behaviors of any assembly of \"bosons\". Einstein's sketches for this project may be seen in the Einstein Archive in the library of the Leiden University.";
To scroll to the end of the text box use
textBox1.SelectionStart = textBox1.Text.Length
This is very simple code for searching text in TextBox and Scrolling.

Blogger Labels: Multiline,TextBox,text,scroll,SelectionStart,example,word,Form,InitializeComponent,sender,EventArgs,ToLower,IndexOf,ScrollToCaret,Select,Focus

1 comment:

  1. Can I modify it in a way that once the specific text reaches to end it starts from the beginning