Click here to Skip to main content
14,934,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having two Forms 'ParentForm' and 'ChildForm'
On 'ParentForm'=) buttonClick 'ChildForm' needs to display like a Popup.
So i wrote like
C#
private void button_Click(object sender, EventArgs e)
       {
           ChildForm popup = new ChildForm();
           DialogResult dialogresult = popup.ShowDialog();
           if (dialogresult == DialogResult.OK)
           {
               popup.Dispose();
           }
       }


But this won't coming in front of ParentForm.
How can i make it display like wise?

What I have tried:

C#
popup.TopMost = true;
popup.Focus();
popup.BringToFront();
popup.Activate();
Posted
Updated 8-Jan-21 15:34pm
v3

ShowDialog makes it a modal form - so any code you write after this line:
C#
DialogResult dialogresult = popup.ShowDialog();
Will not be executed until the user closes the Child form. So if we fix your code so it will compile (you need a space between the type "ChildForm" and the variable name "popup") you should find it appears somewhere and you won't be able to put the focus back to the parent.

And since it's a modal form, it will be above the parent provided you set a location for it to appear in. So start like this:
C#
ChildForm popup = new ChildForm();
popup.StartPosition = FormStartPosition.CenterParent;
if (popup.ShowDialog(this) == DialogResult.OK)
   {
   ...
   
Comments
Member 14978771 8-Jan-21 11:51am
   
Couldn't make any difference :(
OriginalGriff 8-Jan-21 12:26pm
   
What - exactly - happened?

Given that your sample code didn't compile, are you sure that any of it is running at all? What does the debugger show you?
Member 14978771 12-Jan-21 10:13am
   
Actually the PopUp is Showing but just under the Main Form. Exactly want to load the ChildForm Foreground to the ParentForm.
you will get improved behavior by using
popup.ShowDialog(this);
which gives ownership of the child form to the parent form, creating a stronger relationship between both forms.

BTW: whether to call Dispose() or not should not depend on the return value!

And then, a better way to call Dispose() automatically is by using a using statement, as in:

C#
private void button_Click(object sender, EventArgs e) {
   using (ChildForm popup = new ChildForm()) {
       DialogResult dialogresult = popup.ShowDialog(this);
       if (dialogresult == DialogResult.OK) {
           ... // do something with popup result properties if and as required
       }
   }
}

This will always dispose of the ChildForm when you leave the using block, even when an Exception gets thrown.

:)
   
v6
Comments
BillWoodruff 9-Jan-21 9:05am
   
+5
private void button_Click(object sender, EventArgs e)
{
   ChildForm popup = new ChildForm();
   DialogResult dialogresult = popup.ShowDialog();
   if (dialogresult == DialogResult.OK)
   {
       popup.Dispose();
   }
}
You create a new ChildForm instance inside a Button Click EventHandler. Outside the scope of the Button Click EventHandler that instance doesn't exist, and you have no access to what the DialogResult was.

Since this is ASP.NET, you need to clarify if you are using web Forms ... "ASP.Net WebForms is designed to only have one form per page" ...or new Windows. Define clearly what you mean by "Parent Child" here.

Do you need to show the "other" Form modally ?
   
v2

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