Opening a Form from Another Form C# - c#

I have forms 'Form1','Form2' and a mdi. Mdi contains a splitcontainer. In split container there is two panels first one for menu and the other for displaying forms when we click on the menu. My issue is I want to call Form2 from Form1 when I click on an icon in the Form1. I wrote the bellow code in Form1 's icon click. But the Form2 is not showing. I wrote another alternative code like Form2.show() in this case Form2 is displaying but not fit in the panel2 of the Mdi. It is displaying like a popup.
This is the code that I wrote in the Form1 icon click.
private void icon_Click(object sender, EventArgs e)
{
this.Close();
Form2 obj2 = new Form2 ();
obj2 .Show();
obj2 .Location = new Point(0, 0);
obj2 .TopLevel = false;
Mdi Objmdi = new Mdi();
Objmdi.splitContainerControl1.Panel2.Controls.Add(obj2); Objmdi.splitContainerControl1.Panel2.Controls["Form2"].BringToFront();
}

Create an object reference to form 2 in the button_click event and call the 'show' function.
Form1 main = new Form1();
main.Hide();
Form2 second = new Form2();
second.Show();
second.Width = this.Width;
second.Height = this.Height;
second.StartPosition = FormStartPosition.Manual;
second.Location = new Point(this.Location.X, this.Location.Y);
this.Visible = false;

If you want your Form2 to be displayed in some panel instead of a new window then you should create a UserControl instead of a Form and then you should be able to call Objmdi.splitContainerControl1.Panel2.Controls.Add(myNewUserControl);

When a form is closed, all resources created within the object are closed and the form is disposed.
So when you call this.Close(); code after this is not executed.
Change it to this.Hide();.
OR
Change it to this.Hide(); and pass Form1's reference to Form2, which should close it.
Also,
You are creating a new MDI in the below line:
Mdi Objmdi = new Mdi();
Objmdi.splitContainerControl1.Panel2.Controls.Add(obj2); Objhome.splitContainerControl1.Panel2.Controls["Form2"].BringToFront();
Instead of creating a new MDI use existing MDI's reference to do this operation.
Thirdly, Controls["Form2"] try changing to Controls["obj2"]

Related

How to return a string from child form to main form (not open any new form)?

My problem is:
Main form is opened first, there is a button to open Child form. I use Constructor and I want return a string from Child form to the textbox on Main form, and my code now is:
Form1 f = new Form1(txt1.Text);
f.Show();
But, a new main form will open up, the string won't fill in the first main form.
So how to work with only one main form?
You can do this, get it via OpenForms:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
Make sure you declare your control txt1 as public on its Modifier property so that you can access it from Child form:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
frm.txt1.Text = "Change";
frm.Show();
Now if you are trying to change it via its constructor, I doubt you can do it since it was already initialize, unless you call another new Form1 to initialize it and will go through its constructor again.
What you can do is change its property directly:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
frm.stringMain = "Foo"; //Your property you want to change
frm.txt1.Text = "Change";
frm.Show();

how to show form in mdiparent from button in another form by C#

I have two form ( form1 and form2 ) and mdiparent .
button1 in form1
when click this button I want show form2 in mdiparent
The key points when you want to show a form in an MDI Parent, are:
You should have an form with property IsMdiContainer set to true
You should show your mdi parent form
When you want to show a form as mdi child, Set the propetry MdiParent of your child form to and instance of your mdi parent form
So if your Form1 is showing as mdi Child, in button click handler of your form 1, you can simply do this:
var f = new Form2();
f.MdiParent = this.MdiParent;
f.Show();
else, if your mdi parent form is open but form 1 is not mdi child:
var f = new Form2();
//I supposed that [mdiparent] is class name of your mdi parent form
f.MdiParent = Application.OpenForms.OfType<mdiparent>().FirstOrDefault();
f.Show();
esle you should show your mdi parent form first and use above code to show form 2 as mdi child.
The only thing you need is to create a Form2, set it's MdiParent property and show it. The only problem is dynamically setting MdiParent property - you will need to hold the instance of MdiParent. There are several ways to do this "properly".
Simple way
In Form1 button click should have the following event handler:
private void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.MdiParent = this.MdiParent; // "this" is Form1
form.Show();
}
This solution is less architectural - however, you can choose this one if it is suitable.
Singleton solution
If I did this, I would use singleton pattern. That's how I would do this:
MdiParent:
public class MdiParent : Form
{
private static MdiParent _instance;
public static MdiParent Instance
{
get { return _instance ?? (_instance = new MdiParent()); }
}
}
In the place where you instantiate your MdiParent:
MdiParent.Instance.Show();
// instead of
new MdiParent().Show();
If it is a main form - Main in Program.cs:
Application.Run(MdiParent.Instance);
// instead of
Application.Run(new MdiParent());
Form1 button click event:
private void Button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.MdiParent = MdiParent.Instance;
form2.Show();
}

MDI Child Forms Opening each other with the same parent form

So I have 3 Forms, lets call them Form1, Form2, and Form3.
I have sent the IsMDIParent Property to true for Form1.
When I launch the app, It loads Form2 as an MDI Child using
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
And that works fine. What I then want to do is click a button withing the 2nd form that will close Form2 and open up Form3 as a child form of Form1.
I tried
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = Form1;
SecondFormMDI.Show();
on the button click event in Form2, but it would not work.
Do I have to always launch a Child form from the parent form? and if so, how would i go about doing that when it is on the button click event on a child form?
Just use this.MdiParent, instead of Form1, like
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = this.MdiParent;
SecondFormMDI.Show();
You can set the MDIParent of any form in design time, why do it in run-time?
Simply set the value of MDIParent property of Form2 and Form3 to Form1, and that's it.
You could create a method in your MDIForm to open a childform:
public void OpenForm(Form form)
{
form.MdiParent = this;
form.Show();
}
When you want to open a new form in another form you do something like this (example in ChildFormOne with button):
private void btnOpenChildFormTwo_Click(object sender, EventArgs e)
{
((MDIForm)this.MdiParent).OpenForm(new ChildFormTwo());
this.Close();
}
Hope this helps.
ChildForm frmChild = new ChildForm();
frmChild.MdiParent = this.MdiParent;
frmChild.Dock = DockStyle.Fill();
frmChild.Show();

Opening a child form from another child form and set MDI to parent form - how to do?

I have a MDI form. within this MDI form I can open some child forms using:
This is within MainForm
Form1 f1 = new Form1;
f1.MdiParent = this; //this refers to MainForm (parent)
f1.Show();
This works as expected!
But Now, while I am in the child form (Form1 -> f1) I want to open another form as a child for MainForm but when I use this keyword it will reffer to f1. How can I open the new form within f1 and set its MdiParent to MainForm ?
Try assigning the parent form of your first child from:
Form2 f2 = new Form2;
f2.MdiParent = this.ParentForm; //this refers to f1's parent, the MainForm
f2.Show();
Hope this helps.
Let us suppose that the second form is f2.Then, the code in form f1 to create a new form f2 in MDI parent form will be:
Form2 f2 = new Form2;
f2.MdiParent = this.MdiParent;
f2.Show();
Well, not to argue with the "solution" that was listed... but if I'm understanding the request correctly and trying the above solution didnt work i would do the following....
Form2 f2 = new Form2();
f2.MdiParent = MDIParent1.ActiveForm;
f2.Show();
Let us suppose that the second form is frm2.Then, the code in form frm1 to create a new form frm2 in MDI parent form will be: create new object then again retrived data mdiparent forms solved freeze dispose form
Dim dru as New frm2 '// another form call
dru = New frm2
dru.mdiparent = frm1 '// main forms
dru.show()
I had the same problem and tried all different solutions. Finally the one that worked for me was:
Dim ChildForm As New AddingText("")
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = MDIParent1
ChildForm.Dock = DockStyle.Fill
MDIParent1.m_ChildFormNumber += 1
ChildForm.Text = "Client Existent" & MDIParent1.m_ChildFormNumber
ChildForm.Show()
the hiccup is that could not be used in conjunction with ShowDialog(), but i can live with it.

How to open a form within a form?

I have a Parent form and i like to open a child form within the the parent form.
Can this be done? If yes please reply me with sample code .
Thanks !
Following is the code to do what you want:
Assume that button1 is in the parent form.
private void button1_Click(object sender, EventArgs e)
{
this.IsMdiContainer = true;
Form Form2 = new Form();
Form2.MdiParent = this;
Form2.Show();
}
Also the following link will provide you more better details of what you want to do:
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
Hope this helps...
I note that all the answers here assume the OP intended to use MDI Form architecture, although that's never explicitly stated.
And there is another way a Form can be made a 'Child' of another Form: by simply setting its 'TopLevel property to 'False, and then setting its 'Parent property to the other Form.
Form2 f2 = new Form2();
f2.TopLevel = false;
f2.Parent = someOtherForm;
f2.Show();
By the way I think the whole idea of 'Forms within Forms' is a BAD idea, and MDI Architecture is now, justifiably, deprecated by MS.
Much better, I believe, to make secondary Forms 'Owned, and if you must have other Containers inside a Form, use UserControls, Panels, etc.
It depends on what you mean by "within the form". If you need to have the child form shown as a control of the parent form I guess you could try ParentForm.Controls.Add(new ChildForm()). Or maybe even place the child form in an existing container in the parent form by again using the containing control's Controls collection.
HTH
inform child form that its MdiParent is current form.
MDI:
form2 frm = new form2 ();
frm.MdiParent = this;
frm.Show();
Modal dialog:
var form = new Form1();
form.Parent = this;
form.ShowDialog();
MDI child:
var newMDIChild = new Form1();
newMDIChild.MdiParent = this;
newMDIChild.Show();
Form child = new Form();
child.MdiParent = this;
child.Show();
Write these lines of code in parent form and check.
var childform = new form2();
childform.TopLevel=false;
this.Controls.add(childform);
childform.Show();
This works for me.

Categories

Resources