Skip to content

Instantly share code, notes, and snippets.

@akfish
Created September 3, 2011 03:47
Show Gist options
  • Save akfish/1190519 to your computer and use it in GitHub Desktop.
Save akfish/1190519 to your computer and use it in GitHub Desktop.
C# Get active TextBox and insert text at cursor
if (this.ActiveControl.GetType() == typeof(TextBox))
{
TextBox textBox = (TextBox)this.ActiveControl;
String text = ((Label)sender).Text;
int pos = textBox.SelectionStart;
textBox.Text = textBox.Text.Insert(pos, text);
//Fix cursor position
textBox.Focus();
textBox.SelectionStart = pos;
textBox.SelectionLength = text.Length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment