c# winforms events restore textbox contents on escape

Posted by aj3jr on Stack Overflow See other posts from Stack Overflow or by aj3jr
Published on 2010-05-18T17:25:08Z Indexed on 2010/05/18 17:30 UTC
Read the original article Hit count: 209

Filed under:
|
|
|
|

Using c# in 2008 Express. I have a textbox containing a path. I append a "\" at the end on Leave Event. If the user presses 'Escape' key I want the old contents to be restored. When I type over all the text and press 'Escape' I hear a thump and the old text isn't restored. Here what I have so far ...

    public string _path;
    public string _oldPath;

        this.txtPath.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtPath_CheckKeys);
        this.txtPath.Enter +=new EventHandler(txtPath_Enter);
        this.txtPath.LostFocus += new EventHandler(txtPath_LostFocus);

    public void txtPath_CheckKeys(object sender, KeyPressEventArgs kpe)
    {           if (kpe.KeyChar == (char)27)
        {
            _path = _oldPath;
        }
    }

    public void txtPath_Enter(object sender, EventArgs e)
    {
        //AppendSlash(sender, e);
        _oldPath = _path;
    }
    void txtPath_LostFocus(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        AppendSlash(sender, e);
    }
    public void AppendSlash(object sender, EventArgs e) 
    {
        //add a slash to the end of the txtPath string on ANY change except a restore
        this.txtPath.Text += @"\";
    }

Thanks in advance,

© Stack Overflow or respective owner

Related posts about escape

Related posts about key