How can I place a TextBox in overwrite mode instead of insert mode

Platform: WinForms| Category: TextBox

You can handle the textbox’s KeyPress event and if you want the keypress to be an overwrite, just select the current character so the keypress will replace it. The attached sample has a derived textbox that has an OverWrite property and a right-click menu that allows you to toggle this property.

The snippet below shows you a KeyPress handler that automatically does an overwrite if the maxlength of the textbox has been hit. This does not require a derived class.

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
  if(textBox1.Text.Length == textBox1.MaxLength && textBox1.SelectedText == '''')
  {
    textBox1.SelectionLength = 1;
  }
}

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.