开发者

restore previous entry of textbox C#

am doing validating textbox as below,suppose overwrite textbox entry with an invalid number message will pop up,after clicking "OK" I want previous valid entry which is overwritten to be displayed.

how can i do that in C#?

开发者_开发问答private void cor_o_gain_Validating(object sender, CancelEventArgs e)
{
    try
    {
        int entered = int.Parse(cor_o_gain.Text);
        if (entered > 255)
        {
            e.Cancel = true;
            MessageBox.Show("Enter the number between 0 and 255");
        }
    }
    catch (FormatException)
    {
      //  e.Cancel = true;
    }
} 


You can also simply call the Undo() method (in case you are talking about the Windows Forms TextBox)


There is no default built-in way to do that. You'll just have to save the old value yourself (in some variable) and then restore it manually.


Use DataBinding and it will do it for you and it has a method for parsing data that will allow you to perform your validation


Why don't you simply store the current text when the Validated event occures, so you can use it next time?


as said by Codymanix

replace e.Cancel = true; to cor_o_gain.Undo();

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜