Suggestion for controlling the event firing sequence?

G

Guest

I have a 'Controller' object that manages the interaction between its two
children: a Form and a DataObject. I've written specialized code in the
Controller to control how the Form updates the DataObject and vice-versa.

Normally after the user changes the value of a control, the DataObject is
instantaneously updated (via code in the Controller). But I'm having
problems when say the user enters text into a textbox and then immediately
clicks on the upper-right 'X' to close the form. In the Form's Closed event
I tried executing a Form.Focus() method to try to first force the DataObject
to be updated but the update didn't occur until AFTER the Closed event was
finished. Even the Form's Deactivated event occurred before the update.

How can I force the DataObject update to occur first and then close the form
second?
 
G

Guest

I think I've found a solution that works well. Hopefully it'll help out
somebody else in the future. I interecepted the child form's "Closing"
event, which occurs before the "Closed" event. And then I called a method in
the child form that forcibly changes the focus of the control to another
control that can accept focus.

Note: In my case, every child form has a TabControl on it, but the same
principle would work if every control were just sitting on the form itself.

Here's the code:

// This method is in the Controller
private void ChildFormClosing(object sender, CancelEventArgs e)
{
this.childForm.ForceLostFocusEvent(); // Simply forces focus to
another control
}


// This method is in the Child Form
public void ForceLostFocusEvent()
{
foreach (Control control in tabMain.SelectedTab.Controls)
{
if (control.CanFocus && ! control.ContainsFocus)
{
control.Focus(); // This is just an arbitrary control that isn't
the control with the current focus
break;
}
}
}
 

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