C# - Desktop App: Windows form, canceling changes in a control.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TonFrere
    New Member
    • Nov 2007
    • 14

    C# - Desktop App: Windows form, canceling changes in a control.

    Hello,
    I'm building a solution using Visual Studio Windows Forms and I'm coding in C#.

    I have a windows form with databinded textboxes, comboboxes and a datagrid. Everything works fine until a user enter a wrong value in a textbox binded to a numeric field on the form. There is no way to get back to the old value by pressing escape (canceling changes).

    Strangely, this works fine with the datagridview columns: if there is an error in a field, the user can simply hit escape and the cell gets back to its original value.

    Is there any way I can do that with common form textboxes?

    Thanks,
    Justin
  • TonFrere
    New Member
    • Nov 2007
    • 14

    #2
    I hope to help other users by posting a solution to my own problem.

    First of all, the problem I had with using the escape key to undo changes was entirely my fault since I mistook two controls of the same name when testing the Undo() method of the textbox control. Anyways, here is how it's done:

    On the keydown event, call the undo method if the key is "escape"
    if (e.KeyCode == Keys.Escape)
    no_Comm_IntText Box.Undo();

    I also read a Chapter on Validating data input and Handling Errors in Brian Noyes book "Data Binding with Windows Forms 2.0" -Which I strongly recommend for anyone who would like to avoid a whole lotta work by using databinding.

    In this chapter, I learned how to use the ErrorProvider control combine with the "AutoValida te" property and "ValidateChildr en" method of the form. This way, users are not suck in a control if there is an error but they are noticed and they can't save changes until corrections are done.

    Justin

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by TonFrere
      I hope to help other users by posting a solution to my own problem.

      First of all, the problem I had with using the escape key to undo changes was entirely my fault since I mistook two controls of the same name when testing the Undo() method of the textbox control. Anyways, here is how it's done:

      On the keydown event, call the undo method if the key is "escape"
      if (e.KeyCode == Keys.Escape)
      no_Comm_IntText Box.Undo();

      I also read a Chapter on Validating data input and Handling Errors in Brian Noyes book "Data Binding with Windows Forms 2.0" -Which I strongly recommend for anyone who would like to avoid a whole lotta work by using databinding.

      In this chapter, I learned how to use the ErrorProvider control combine with the "AutoValida te" property and "ValidateChildr en" method of the form. This way, users are not suck in a control if there is an error but they are noticed and they can't save changes until corrections are done.

      Justin

      Thanks for share the solution Justin

      Comment

      Working...