What event do I use to indicate that data has been edited?

D

dbuchanan

Hello,

I need some help in understanding what events to use to detect if the user
has begun to edit the data on the form (dialog).

I want to detect this because I am intending to change the configureation of
the form depending on its current state. (For one thing, I want the "Save"
button not to be enabled until after the edits begin.)

I have encountered numerous problems trying to do this because the events
want to fire at the wrong times.


Overview of my setup:
----------------------------------------------------
A button on a parent form opens the child form (the form which is the topic
of this post).

I am using events associated with the controls on the child form to detect
if the user has made changes to data; for text boxes I use the "TextChanged"
event, and for check boxes I use the "CheckChanged" event.

All the events (because of multiple controls) call a single method which in
turn calls the btnConfigure method. (see snippit #1)

You will notice that I use a boolean variable (_blnFormLoading) to handle
the form loading. (I don't want the btnConfigure metod to run during the
loading of the form)

The code in snippit #1 calls btnConfigure (seen in snippit #2) which has
case statements to select the appropriate code depending on the desired
state of the form.

The use of the enum seen in snippit #3 will be understood.

The child form is a used as a dialog to edit or add new records to the
dataGridView on the parnet form.


The problem:
----------------------------------------------------
Although I thought I made provisions so that the events would not run
btnConfig while the form was loading it does not prevent the events from
firing during the showing of the form which is run from this statement
"f.ShowDialog()." Each control in turn executes the btnConfig method. This
is not what I want!

TO try to fix this I changed the code by putting the statement
"_blnFormLoading = false;" within the form activate instead of as the last
statement of my constructor. This did not work either. It unexpectedly fires
numerous times. Not only for FrmLoadToEdit, but also numerous times for
EditBegun. (see enum for meaning of these states)

I do not understand this behavior at all!?!
Why does ShowDialog change the text in all the controls?
Are not the controls populated during the form load?
If the TextChanged event is fired during the ShowDialog then what event
should I be using to only respond to user control edits?!?!


Allow me to think and type at the same time...
----------------------------------------------------
I don't think I should be using the Entering or Leave event because that
does not mean data has changed.

KeyPress might work but that seems like it is there for an entirely
different purpose.

I experimented with the events of the BindingSource but that seemed to have
nothing to offer unless I use Control.Leave in conjuction with
BindingSource.BindingComplere. For example when BindingComplete follows a
Leave then there is an edit. But that is not good enough because the event
has to be initiated _before_ the user leaves the control so that the Save
button can be enabled upon recognizing an edit has occured.

This seems to come back to something like control entry in combination with
keypress or something. Could you please give me some insight into what is
the proper way to do this.

Again, what I want to do:
Control the state of the form depending on user action such as not enabling
the Save button until edits have actually ocurred.

Or is this all just as simple as resigning myself to enabling the save
button when the user clicks inside a control?

Thank you,

Douglas

====================================================
Snippits
====================================================
== Snippit #1 from the derived form ==

public void EditsBegun(object sender, EventArgs e)
{
// Change buttons after edits begin
if (!_blnFormLoading)
{
myDlgState = dlgState.EditBegun;
btnConfigure();
}
}

== Snippit #2 from the derived form ==
== code run to configure the desired state of the form

private void btnConfigure()
{
switch (myDlgState)
{
case dlgState.FrmLoadToAdd:
btnNew.Enabled = false;
btnDelete.Enabled = false;
btnSave.Enabled = false;
break;
case dlgState.FrmLoadToEdit:
btnNew.Enabled = true;
btnDelete.Enabled = true;
btnSave.Enabled = false;
break;
case dlgState.EditBegun:
btnNew.Enabled = false;
btnDelete.Enabled = false;
btnSave.Enabled = true;
break;
}
}

== Snippit #3 ~ from the base class form ==
== code to provide a way to identify the state of the form

// Static variable to hold Type ~ Accomodates one dialog at a time.
static public dlgState myDlgState;

// Type
public Type dlgMyButtonState = typeof(dlgState);

// Enumeration for Dialog State
public enum dlgState
{
FrmLoadToAdd, // When no record was present or selected in the calling
form
FrmLoadToEdit, // When a record was present and selected in the calling
form
EditBegun // When user begins to edit data on the form
}
 
L

Linda Liu[MSFT]

Hi Douglas,

To solve your problem, I suggest that you subscribe the TextChanged event
of the TextBox and CheckedChanged event of the CheckBox at the end of the
Load event handler of the form. Thus, the TextChanged event of the TextBox
and the CheckedChanged event of the CheckBox won't be fired during the
form's loading.

The following is a sample. It requires that you add a TextBox, a CheckBox
and a Button on a form.

private void Form1_Load(object sender, EventArgs e)
{
this.btnSave.Enabled = false;
this.textBox1.TextChanged += new
EventHandler(textBox1_TextChanged);
this.checkBox1.CheckedChanged += new
EventHandler(checkBox1_CheckedChanged);
}

void checkBox1_CheckedChanged(object sender, EventArgs e)
{
btnSave.Enabled = true;
}

void textBox1_TextChanged(object sender, EventArgs e)
{
btnSave.Enabled = true;
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dbuchanan

Linda,

It worked. Thank you very much for your help!

I had mistakenly thought that a conditional, set at false until the form had
loaded, would do the trick. Wrong - but I don't understand why.

However I can see that not subscribing until I need the event is infinitely
better.

Thanks,

Douglas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top