views:

28

answers:

2

Ok, I know that the new versions of windows do not use the insert key by default and you have to program for it. I want to be able to type in my text box and override the content that is in it just like in old windows when you could activate the insert key. This is just for my WPF 4, VB.net Application.

Updated Information:

That what I meant: I need to mimic old terminals. I need to activate the overwrite mode programmatically for all the controls. The same affect as the 'Insert' key on the keyboard. Only that key does not work in a WPF environment.

Example I am entering the word world over a text box that says 'Hello!':

Textbox Started as: [Hello!]

The Textbox is now [World!]

You will note that the one character exclamation mark stayed because world is not enough characters to replace the '!'.

+1  A: 

In WPF 4 pressing Insert key when textbox has focus activates overwrite mode. Do you mean changing between Insert and Overwrite modes for all textboxes on a window at once?

rook
Yes, I need to be able to set it programmaticllly for every textbox (or like) control.
Justin
Anyone have any ideas.
Justin
+1  A: 

Try this out (use this control instead of vanilla TextBoxes):

public class InsertModeTextBox : TextBox
{
    public InsertModeTextBox()
    {
        InitializeComponent();
    }

    protected override void OnTextInput(TextCompositionEventArgs e)
    {
        var txt = e.Text;
        var len = txt.Length;
        var pos = CaretIndex;

        var builder = new StringBuilder();
        if (pos > 0) builder.Append(Text.Substring(0, pos)); // text before caret
        builder.Append(txt); // new text
        if (Text.Length > pos + len) builder.Append(Text.Substring(pos + len)); // text after overwrite

        Text = builder.ToString();
        CaretIndex = pos + len;

        e.Handled = true;
    }
}
Jay
Its got a bug or two, but thank you it solves my problem.
Justin