473,290 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,290 software developers and data experts.

Validation and Undo

General question about how WinForms handles undoing a change during a
control's validation, if it does at all.
After a change to a control's value, if the data is determined to be
invalid, then in the control's Validating event handler, setting e.Cancel =
true will prevent the offending control from losing focus until the data in
the control is good.

If the user wants to revert to the original data, say by hitting ESC, does
the form have any kinds of default behaviours for this kind of action (ie:
undoing a change)? Or do I have to program this whole undo process
manually?

This could entail overriding the ProcessCmdKey and looking for ESC, and
storing the value of the control before the control receives focus so I can
reset it after ESC is hit.

I had thought that this was a default behaviour for some reason, but testing
this out reveals this not to be the case for my TextBox.

Are there any suggestions for a better way to implement a simple undo?
Dec 14 '05 #1
10 4276
John,

The TextBox has undo capability, but you don't know how many changes
have been made, so you just can't unwind the undo stack.

Rather, you will have you go to the data source and get the original
value (or, store the original value when binding occurs). That's the only
way you can be sure of the original value.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Richardson" <j3*********@nospam.ca> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
General question about how WinForms handles undoing a change during a
control's validation, if it does at all.
After a change to a control's value, if the data is determined to be
invalid, then in the control's Validating event handler, setting e.Cancel
= true will prevent the offending control from losing focus until the data
in the control is good.

If the user wants to revert to the original data, say by hitting ESC, does
the form have any kinds of default behaviours for this kind of action (ie:
undoing a change)? Or do I have to program this whole undo process
manually?

This could entail overriding the ProcessCmdKey and looking for ESC, and
storing the value of the control before the control receives focus so I
can reset it after ESC is hit.

I had thought that this was a default behaviour for some reason, but
testing this out reveals this not to be the case for my TextBox.

Are there any suggestions for a better way to implement a simple undo?

Dec 14 '05 #2
>The TextBox has undo capability

Is this only for a databound context? I looked at the interface for the
textbox and I couldn't see where the previous value of the control would be
stored... and I don't need a whole undo history. That being said, I'll
probably build it myself anyways to give myself some flexibility.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uF*************@tk2msftngp13.phx.gbl...
John,

The TextBox has undo capability, but you don't know how many changes
have been made, so you just can't unwind the undo stack.

Rather, you will have you go to the data source and get the original
value (or, store the original value when binding occurs). That's the only
way you can be sure of the original value.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Richardson" <j3*********@nospam.ca> wrote in message
news:uU**************@TK2MSFTNGP11.phx.gbl...
General question about how WinForms handles undoing a change during a
control's validation, if it does at all.
After a change to a control's value, if the data is determined to be
invalid, then in the control's Validating event handler, setting e.Cancel
= true will prevent the offending control from losing focus until the
data in the control is good.

If the user wants to revert to the original data, say by hitting ESC,
does the form have any kinds of default behaviours for this kind of
action (ie: undoing a change)? Or do I have to program this whole undo
process manually?

This could entail overriding the ProcessCmdKey and looking for ESC, and
storing the value of the control before the control receives focus so I
can reset it after ESC is hit.

I had thought that this was a default behaviour for some reason, but
testing this out reveals this not to be the case for my TextBox.

Are there any suggestions for a better way to implement a simple undo?


Dec 15 '05 #3
"John Richardson" <j3*********@nospam.ca> a écrit dans le message de news:
O$**************@TK2MSFTNGP14.phx.gbl...

| >The TextBox has undo capability
|
| Is this only for a databound context? I looked at the interface for the
| textbox and I couldn't see where the previous value of the control would
be
| stored... and I don't need a whole undo history. That being said, I'll
| probably build it myself anyways to give myself some flexibility.

I can't see the previous messages in this thread, has the subject changed ?

In the absence of other replies, can I suggest you look at implementing
IEditableObject in the class whose instances you are editing. If you are
linking to a database table, then I thought that the record datatype had
undo facilities. It is not the job of the edits to maintain undo
information, that is the job of the class supplying the data.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 15 '05 #4
>It is not the job of the edits to maintain undo information, that is the
job of the class supplying the data.
I think that I might disagree with this. I think that there should be 2
levels of editing/undoing: the individual field, and the "row", or more
generally, a set of fields. A typical form interaction will be to load
data, edit various fields, and then indicate that the data should be
saved -> this is the transaction that should be class level.

But, as the user edits a field, the control itself offers a Validation
event, which is hinting at a Transaction at the control level. So, a Field
transaction should be viewed as beginning when the control is focused,
ending when the control is unfocused, and I would think that a cancel should
be implemented by ESC, or some other commonly understood method.

For some reason, I figured this was a default behaviour of the Control
object, but it is not. In my view, this behaviour is fairly natural
assumption... it makes more sense than say the SHIFT-SPACE behaviour on the
datagrid, that is for sure.
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:uM**************@TK2MSFTNGP10.phx.gbl... "John Richardson" <j3*********@nospam.ca> a écrit dans le message de news:
O$**************@TK2MSFTNGP14.phx.gbl...

| >The TextBox has undo capability
|
| Is this only for a databound context? I looked at the interface for the
| textbox and I couldn't see where the previous value of the control would
be
| stored... and I don't need a whole undo history. That being said, I'll
| probably build it myself anyways to give myself some flexibility.

I can't see the previous messages in this thread, has the subject changed
?

In the absence of other replies, can I suggest you look at implementing
IEditableObject in the class whose instances you are editing. If you are
linking to a database table, then I thought that the record datatype had
undo facilities. It is not the job of the edits to maintain undo
information, that is the job of the class supplying the data.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 15 '05 #5
"John Richardson" <j3*********@nospam.ca> a écrit dans le message de news:
%2****************@TK2MSFTNGP09.phx.gbl...

| But, as the user edits a field, the control itself offers a Validation
| event, which is hinting at a Transaction at the control level. So, a
Field
| transaction should be viewed as beginning when the control is focused,
| ending when the control is unfocused, and I would think that a cancel
should
| be implemented by ESC, or some other commonly understood method.
|
| For some reason, I figured this was a default behaviour of the Control
| object, but it is not. In my view, this behaviour is fairly natural
| assumption... it makes more sense than say the SHIFT-SPACE behaviour on
the
| datagrid, that is for sure.

FMPOV, the Validating event in the Control class fits very well into the
Model View Presenter framework as it is a response to a user interaction
trying to quit editing a value.

The edit is a means of entering data that will be passed to the underlhying
data member. The Validating event gives the program an opportunity to
interrupt the flow of data to the data member and prompt the edit to refresh
itself to the, as yet unchanged, underlying data member.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 15 '05 #6
I completely agree with what you wrote below. But it doesn't quite address
what I was asking about originially. I was using the Validation event as an
example, but my original post was about how the user can indicate aborting
an edit of an individual field, which would then undo the value -> going
back to the original previous to the edit. In this case, the *standard*
Validation event doesn't apply because the control hasn't lost focus yet.
The user is indicating that a mistake was just made, and the original value
of the field should be restored. Admittedly, this is very subtle, since
it's very similar to just forcing a failed validation procedure.

To continue your Model View Presenter paradigm, it seems useful to have a
kind of UserCancelled message to cause the display of the control to be
reloaded with previous data; it differentiates a user-initiated cancel from
failing a logic test in the Validating handler when the user tries to enter
bad data. If this is in fact a good idea, then I guess I'm surprised that
the general control interface doesn't provide any implicit methods for this.

<Tab> will change focus. Why not <ESC> to revert to the value before focus.
I guess maybe the Form is capturing Esc msgs...
Well, either way, I'll have to code what I want myself.

It's been a fun discussion.

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:eT**************@TK2MSFTNGP11.phx.gbl...
"John Richardson" <j3*********@nospam.ca> a écrit dans le message de news:
%2****************@TK2MSFTNGP09.phx.gbl...

| But, as the user edits a field, the control itself offers a Validation
| event, which is hinting at a Transaction at the control level. So, a
Field
| transaction should be viewed as beginning when the control is focused,
| ending when the control is unfocused, and I would think that a cancel
should
| be implemented by ESC, or some other commonly understood method.
|
| For some reason, I figured this was a default behaviour of the Control
| object, but it is not. In my view, this behaviour is fairly natural
| assumption... it makes more sense than say the SHIFT-SPACE behaviour on
the
| datagrid, that is for sure.

FMPOV, the Validating event in the Control class fits very well into the
Model View Presenter framework as it is a response to a user interaction
trying to quit editing a value.

The edit is a means of entering data that will be passed to the
underlhying
data member. The Validating event gives the program an opportunity to
interrupt the flow of data to the data member and prompt the edit to
refresh
itself to the, as yet unchanged, underlying data member.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 16 '05 #7
"John Richardson" <j3**********@hotmail.com> a écrit dans le message de
news: OC**************@TK2MSFTNGP14.phx.gbl...

|I completely agree with what you wrote below. But it doesn't quite address
| what I was asking about originially. I was using the Validation event as
an
| example, but my original post was about how the user can indicate aborting
| an edit of an individual field, which would then undo the value -> going
| back to the original previous to the edit.

| <Tab> will change focus. Why not <ESC> to revert to the value before
focus.
| I guess maybe the Form is capturing Esc msgs...

If you implement IEditableObject in the business object whose properties you
are trying to edit, then pressing the Esc key whilst still in the edit will
revert the value in the edit to that of the underlying property. Is that
what you want ?

| Well, either way, I'll have to code what I want myself.

Not necessarily :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 17 '05 #8
That is precisely what I want, but I realise now that it can only be
acheived with databinding, which is not quite what I want, since I don't use
databinding where I can avoid it. It's interesting, because online
documentation says nothing about relating ESC, IEditableObject, and any
generic control. Most google hits discuss the DataGrid, which has several
special key behaviours defined. If this works for a textbox, I will be a
bit surprised.

Thanks for going the distance.
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:uk**************@TK2MSFTNGP12.phx.gbl...
"John Richardson" <j3**********@hotmail.com> a écrit dans le message de
news: OC**************@TK2MSFTNGP14.phx.gbl...

|I completely agree with what you wrote below. But it doesn't quite
address
| what I was asking about originially. I was using the Validation event
as
an
| example, but my original post was about how the user can indicate
aborting
| an edit of an individual field, which would then undo the value -> going
| back to the original previous to the edit.

| <Tab> will change focus. Why not <ESC> to revert to the value before
focus.
| I guess maybe the Form is capturing Esc msgs...

If you implement IEditableObject in the business object whose properties
you
are trying to edit, then pressing the Esc key whilst still in the edit
will
revert the value in the edit to that of the underlying property. Is that
what you want ?

| Well, either way, I'll have to code what I want myself.

Not necessarily :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Dec 20 '05 #9
"John Richardson" <j3*********@nospam.ca> a écrit dans le message de news:
eQ**************@TK2MSFTNGP15.phx.gbl...

John, I must admit, having gone to the trouble to design a good working
BindingList and implementing all the right interfaces to support this
behaviour in a DataGridView, I had expected it to work with the edits as
well.

Surprise, surprise !! Thanks to you, I now need to delve into the inward
parts of the FCL and see why this is not happening; it is a functionality
that my client requires as well.

If you should find the answer before I do, please be good enough to drop me
a note as well as posting it here. my address is my first name at
carterconsulting, the domain is .org.uk.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 20 '05 #10
John, I just found out the following *horrible* idea to allow for undoing :

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape && textBox1.CanUndo)
{
textBox1.Undo();
textBox1.ClearUndo();
}
}

....or you can do something sensible like creating an Extender Provider.

This will get added to your toolbox and you can add one of them to any form;
it will hook into the KeyDown event of any and all TExtBoxBase derivatives
on the form if you set the UndoOnEsc property of each TextBox to true :

////////////////////////////
using System;
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;

namespace // your namespace
{
[ProvideProperty("UndoOnEsc", typeof(TextBoxBase))]
public class EditUndoProvider : Component, IExtenderProvider
{
private class Properties
{
private bool undoOnEsc = false;

public bool UndoOnEsc
{
get { return undoOnEsc; }
set { undoOnEsc = value; }
}
}

private Hashtable properties = new Hashtable();

public EditUndoProvider(IContainer parent)
{
parent.Add(this);
}

bool IExtenderProvider.CanExtend(object obj)
{
return obj is TextBoxBase;
}

private Properties EnsurePropertiesExists(object key)
{
Properties p = (Properties) properties[key];

if (p == null)
{
p = new Properties();

properties[key] = p;
}

return p;
}

[Category("Behavior")]
[Description("Allows Undo when Esc key is pressed")]
[DefaultValue(false)]
public bool GetUndoOnEsc(Control c)
{
return EnsurePropertiesExists(c).UndoOnEsc;
}

public void SetUndoOnEsc(Control c, bool value)
{
EnsurePropertiesExists(c).UndoOnEsc = value;

if (value)
c.KeyDown += HandleKeyDown;
else
c.KeyDown -= HandleKeyDown;
}

private void HandleKeyDown(object sender, KeyEventArgs args)
{
if (!DesignMode)
{
TextBoxBase control = (TextBoxBase) sender;
if (args.KeyCode == Keys.Escape && control.CanUndo)
{
control.Undo();
control.ClearUndo();
}
}
}
}
}
////////////////////////

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: R Bolling | last post by:
I am using a routine to check to see if a phone number (PK) has alread been entered, and takes the user to that record if it is found -- as follows: Private Sub...
2
by: Tony Williams | last post by:
I am validating two text boxes to make sure they contain data. I have use Is Not Null in the Validation Property of the control but it doesn't seem to work. The Help shows Validation with code. How...
2
by: Bart Lateur | last post by:
I'd like to have validation of the inputted text, in a form textbox. I want to give the user the option to try again, or to cancel out after which the textbox is either cleared or restored. I just...
3
by: GoogleEyeJoe | last post by:
Dear ladies and gents, I'm trying to determine whether the .NET Framework implements a means of transactional processing without the need for a database. Essentially, I'd like to enlist...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
Hello, Does vb2005 have a built-in UnDo feature / object for applications so that I can undo actions like other windows apps? Or do I have to write my own UnDo routine? If vb2005 does have a...
5
by: billa856 | last post by:
Hi, My project is in MS Access. In that one form of data entry I am using this code for validation of field. Private Sub CustomerCode_Combo_LostFocus() If Me.CustomerCode_Combo = "" Or...
0
by: wizard of oz | last post by:
Hi all, I'm extending an Abstract Styled Document associated with a JTextPanel to implement a syntax highlighting editor. This is all working just fine - except for undo / redo. The problem...
1
by: Jeremy | last post by:
I'm working on an application that does some DOM manipulation. I want to add a mechanism for "Undo" so that the user can revert to the previous state after performing a mistaken action. Simple...
18
by: ChipR | last post by:
I have a text box with a validation rule and validation text. When entering a new record, if I put in invalid text, the validation text is displayed in a message box, but after clicking OK, another...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.