private void Paste1_Click(object sender, EventArgs e) { // Determine if there is any text in the Clipboard to paste into the text box. if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)) { // Determine if any text is selected in the text box. if (TextBox1.SelectionLength > 0) { // Save selection start point and selction length int SelStart = TextBox1.SelectionStart; int SelLength = TextBox1.SelectionLength; switch (PasteMode) { case PasteModes.BeforeSelection: // Turn off the selection, paste clipboard text then re-show selection // in corrected position after adding the text TextBox1.DeselectAll(); TextBox1.Paste(); TextBox1.SelectionStart = SelStart + Clipboard.GetText().Length; TextBox1.SelectionLength = SelLength; break; case PasteModes.AfterSelection: // Turn of the selection to prevent it getting overwritten, paste clipboard // text, then set selection back to how it was TextBox1.DeselectAll(); TextBox1.SelectionStart = SelStart + SelLength; TextBox1.Paste(); TextBox1.SelectionStart = SelStart; TextBox1.SelectionLength = SelLength; break; case PasteModes.OverSelection: TextBox1.Paste(); break; default: break; } } else { TextBox1.Paste(); } } }