Working With C# Strings: Selection Property

This Program in C# 5 shows the use of the Selection property of the TextBox control. This property is accompanied with the Select() function which must be used to reflect changes you have set to the Selection properties. As you can see, the form also contains two TrackBars. These TrackBars actually visualize the Selection property attributes of the TextBox. There are two Selection properties mainly: Selection Start & Selection Length. These two properties are shown through the current value marked on the TrackBar.


        private void Form1_Load(object sender, EventArgs e)
        {
            // Set initial values
            txtLength.Text = textBox.Text.Length.ToString();
            trkBar1.Maximum = textBox.Text.Length;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            // Set the Max Value of second TrackBar
            trkBar2.Maximum = textBox.Text.Length - trkBar1.Value;

            textBox.SelectionStart = trkBar1.Value;

            txtSelStart.Text = trkBar1.Value.ToString();

            textBox.SelectionLength = trkBar2.Value;

            txtSelEnd.Text =
                (trkBar1.Value + trkBar2.Value).ToString();

            txtTotChars.Text = trkBar2.Value.ToString();

            textBox.Select();
        }


Let us understand the working of this property with a simple String "ABCDEFGH". Suppose you wanted to select the characters from between B & C and Between F & G ("A B  C D E F  G H"), you would set the Selection Start to 2 and the Selection Length to 4. As always, the index starts from 0(Zero) therefore the Selection Start would be 0 if you wanted to start before A i.e. include A as well in the selection.



Notice something below: As we move to the right in the First TrackBar (provided the length of the string remains the same), We find that the second TrackBar has a lower range i.e. a reduced Maximum value.

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            textBox.SelectionStart = trkBar1.Value;

            txtSelStart.Text = trkBar1.Value.ToString();

            textBox.SelectionLength = trkBar2.Value;

            txtSelEnd.Text =
                (trkBar1.Value + trkBar2.Value).ToString();

            txtTotChars.Text = trkBar2.Value.ToString();

            textBox.Select();
        }


This is only logical. When we move on to the right, we have a higher Selection Start value. Therefore the number of selected characters possible will be lesser than before because the leading characters will be left out from the Selection Start property. Therefore its just showing graphically how many characters of the String are showed on a Text Box on the Form. 

 
Please Note :
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net
** All Program Codes written here are 100%  tested & running.