Here is the complete code.
int pos = TextBox1.SelectionStart;
string textToInsert = "-Text to Insert-";
string newText = TextBox1.Text.Substring (0, pos) +
textToInsert +
TextBox1.Text.Substring(pos,
txtFirstName.Text.Length - pos);
TextBox1.Text = newText;
It is completely UNNECESSARY code*. The SelectedText property is all you
need. The only time you would need to use code like above is if you wanted
to handle the situation of text already being selected in the control and
you didn't want to overwrite it. Then you'd have to decide whether you
wanted the new text inserted at the beginning of the selection or after it.
But if you DO want selected text to be replaced, then consider these two
examples:
1) The text box contains "The quick brown fox" and the word "brown" is
selected (highlighted, if you prefer).
textBox1.SelectedText = "red"
The text box now contains "The quick red fox".
2) The text box contains the same text but nothing is selected. The cursor
(or in proper Windows terminology, the caret) is right before the "b" in
"brown."
textBox1.SelectedText = "red"
The text box now contains "The quick redbrown fox". It's that simple.
For reference, under the covers, the EM_REPLACESEL message is being sent to
the control.
*I'm not saying it's wrong; it's just the looooong way.