开发者

How to use the Insert Key (like in traditional windows) for a WPF 4 App?

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 '!'.


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;
    }
}


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜