Reputation: 1399
I have a form (MainForm) with some buttons like btnInsertContact, btnInsertEmployemant, ...
for example when the user click btnInsertContact, another form be shown that user should enter his contact information in it. but the MainForm dont be closed.
I want the event that when user close Contact form, it happend.
by this code, Contact Form be shown, but the MainForm dont be close.
private void btnInsertContact_Click(object sender, EventArgs e)
{
frmContact.ShowDialog();
}
I want to update some information in MainForm, after closing Contact form.
I tried Load event but its not true, because my MainForm didnt Close
I tried Enter event but it didnt work, I dont know why.
what event I should handle?
Upvotes: 1
Views: 1017
Reputation: 3484
if you want to get values from contact form then you should assign controls' modifier to public
//MAIN FORM
//First method
private void btnInsertContact_Click(object sender, EventArgs e)
{
frmContact.FormClosed += new EventHandler(ContactForm_Closed);
frmContact.ShowDialog();
}
private void ContactForm_Closed(object sender, EventArgs e)
{
//Write ur code here
}
//Second method
private void btnInsertContact_Click(object sender, EventArgs e)
{
frmContact.ShowDialog();
write ur code here.
}
Upvotes: 2
Reputation: 1557
Form.ShowDialog()
is a blocking call. The program flow stops at this method until the method returns. The ShowDialog()
method returns when the user closes the form.
You can simply update your information in the main form after calling ShowDialog()
:
private void btnInsertContact_Click(object sender, EventArgs e)
{
frmContact.ShowDialog();
// ToDo: Insert your code for updating the main form here
}
Upvotes: 1
Reputation: 244672
The ShowDialog
function displays a modal dialog box and is therefore blocking. In plain English, that means that the ShowDialog
function will not return (yield execution) until after the dialog box has been closed (either by the user clicking OK, Cancel, or some other button you provide).
So all you need to do is place your synchronization code after the call to ShowDialog
. (Of course, you might want to check the DialogResult
property to ensure that the user clicked the OK or Yes button!)
Upvotes: 2