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
}
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
}