Where to add event handlers so they don't fire during form load

D

dbuchanan

Hello,

Where do I put the code that adds my EventHandlers so that they do not fire
during form load?

========= Here is my code: ==========

public partial class AddEditMasterTask : bsfAddEdit
{
public AddEditMasterTask()
{
InitializeComponent();

// form title
this.Text = "";

// Title
lblDialogTitle.Text = "Add Master Task";

// Populate Dropdowns
taPhase.Fill(dataSetHipAdmin.Phase);
taMeasureDim.Fill(dataSetHipAdmin.MeasureDim);
taMeasureUnit.Fill(dataSetHipAdmin.MeasureUnit);

// Populate form
taMTask.Fill(dataSetHipAdmin.MTask);

// Disable Save button, Add Event Handlers
ButtonSettingsAndEventHandlers();
}

public void ButtonSettingsAndEventHandlers()
{
// Hide Save
this.btnSave.Enabled = false;

// TextChanged
this.descriptionTextBox.TextChanged += new
System.EventHandler(this.EditsBegun);
this.baseTimeTextBox.TextChanged += new
System.EventHandler(this.EditsBegun);
this.ordTextBox.TextChanged += new
System.EventHandler(this.EditsBegun);
this.contractorCommentsTextBox.TextChanged += new
System.EventHandler(this.EditsBegun);

// CheckedChanged
this.allowParticipationCheckBox.CheckedChanged += new
System.EventHandler(this.EditsBegun);
this.allowQuantityCheckBox.CheckedChanged += new
System.EventHandler(this.EditsBegun);
this.configuredDisplayCheckBox.CheckedChanged += new
System.EventHandler(this.EditsBegun);

// SelectedValueChanged
this.fkMeasureDimIDComboBox.SelectedValueChanged += new
System.EventHandler(this.EditsBegun);
this.fkMeasureUnitIDComboBox.SelectedValueChanged += new
System.EventHandler(this.EditsBegun);
this.fkPhaseIDComboBox.SelectedValueChanged += new
System.EventHandler(this.EditsBegun);
}

public void EditsBegun(object sender, EventArgs e)
{
// Change buttons after edits begin
this.btnSave.Enabled = true;
}

===============================
The EditsBegun event fires during form load. What did I do wrong?

Doug
 
M

Mr. Arnold

The EditsBegun event fires during form load. What did I do wrong?

You know when the form is loading. So, you set a blnFormLoading, which
should be Public. When the form is loading, you set blnFormLoading = true.

In EditsBegun, you have an IF statement.

If !blnFormLoading // if it's true, code is not executed.
execute code;

At the end of the Form_Load, you set blnFormLoading = false.

So, while blnFormLoading = true because you have set it to true at the
start for the From_Load, no code in EditsBegun will be executed. EditsBegun
can firer all it wants.

At the end of the Form_Load, you have set blnFormLoading = false. And now
when EditsBegun fires because you do want to handle the event and it's not
at Form_Load, the code will be executed, because the flag is false and
you're not at Form_Load.
 
J

Jeffrey Tan[MSFT]

Hi dbuchanan,

Do you databind these controls with any datasource? If the controls are
bound to the datasource, their change events will be fired when the
DataSource property is set or when the DataBindings.Add() is called.

To prevent the above change events, you should place the
ButtonSettingsAndEventHandlers() method after the databinding code. In my
test project, this will work.

If you have any problem of getting it work, please create a little sample
project to demonstrate your project, then I can give it a debugging to find
out why it does not work on your side.

Thanks.

Best regards,
Jeffrey Tan
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.
 

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