Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Parent form and child form

if a Submit button is clicked on child form and after closing child form it should show messagebox on Parent form like this way "Submit button is clicked on Child Form..."
Posted

Hello

1. In Parent From:

C#
ChildForm child = new ChildForm();

child.ShowDialog();

if (child.DialogResult == System.Windows.Forms.DialogResult.OK)
    MessageBox.Show("Submit button is clicked on Child Form...");



2. In Child From, In Submit_Click event:

C#
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();


Or In Child Form, you can use this instead of Sumbit_Click event:

C#
this.Submit.Click += new EventHandler((object sender, EventArgs e) =>
{
    this.DialogResult = System.Windows.Forms.DialogResult.OK;
    this.Close();
});
 
Share this answer
 
v3
Comments
Ravi Sargam 12-Mar-12 7:14am    
Thx 4 Solutions.....
Shahin Khorshidnia 12-Mar-12 7:33am    
You're welcome.
Ravi Sargam 12-Mar-12 7:46am    
Hey Can you answer my another problem
Shahin Khorshidnia 12-Mar-12 7:50am    
Cristal report?
I like to, but unfortunately not very experianced in cristal report.
Ravi Sargam 12-Mar-12 7:52am    
yess it's ok..... i'm waitring 4 answer..
Here is the sample code

C#
public partial class ParentForm : Form
{
    ChildForm tempDialog = new ChildForm(this);
    tempDialog.ShowDialog();
    public void msgme()
    {
        MessageBox.Show("Parent Function Called");
    }
}

public partial class ChildForm : Form
{
    private ParentForm m_parent;
    public ChildForm(ParentForm frm1)
    {
        InitializeComponent();
        m_parent = frm1;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
        m_parent.msgme();
    }
}
 
Share this answer
 
Comments
Shahin Khorshidnia 12-Mar-12 6:48am    
Not a very good solution to have a constructor with parameter of parent form.

In your solution the child form can not be called without being the specific parent form.

For example an "Add Person Form" can be used as a child of many forms but in your solution, the child form can be actually used as the child of only one specific form.

Sorry for the criticism!
Shahin Khorshidnia 12-Mar-12 7:08am    
And Thank you for DownVoting my solution and the others !
Create a public bool property on your child form. Initialise it to false when the form is shown. Set it true when submit is pressed.
In the parent, after the child is closed, check the property in the child and update parent accordingly.
 
Share this answer
 
Comments
Shahin Khorshidnia 12-Mar-12 6:50am    
Why to need a public bool property when the DialogResult is ready and available?
Zasky 12-Mar-12 7:32am    
Because the dialog result may not be the same. Something could be submitted within the child form, but the user may not want to ok the form. I don't know how the form is meant to behave, do you?
Shahin Khorshidnia 12-Mar-12 7:45am    
Thanks for replay.

1.The question was about a child form with 1 sumbit button.
2. DialogResult has 8 enums, whereas a public bool property has only 2 states. (true and false)
3. A data entry form has normaly 1 submit button.
4. If the child form has many submitable subjects then it's better to have many public DialogResult property instead of "a" public bool property.
Zasky 12-Mar-12 8:08am    
Well, you've made assumptions there. The question says 'a' submit button, so there may be other buttons and, although a data entry form generally has 1 submit button it may not always be so.
DialogResult is just a public property much like any other and if you're assumptions are correct then it would be sensible to use it. I'm not sure what you mean by having many public DialogResult properties. Perhaps you mean that my property could be a DialogResult enum intead of bool? That would be fine but no better than bool in my opinion. This solution just provides another approach which may (or may not!) be a better fit.
Shahin Khorshidnia 12-Mar-12 8:57am    
Ok dear Zasky, I think, we need to think about each other's solution :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900