How to open a form within a form? - c#

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.

Related

Opening a Form from Another Form 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"]

C#, WinForm in another winform

I need help. I have winform, like one which is shown on picture, so I want to improve next things, when I click on first button, on the right side will be showed content from another winform, is this possible? I don't want to use panels.
Why don't you just embed a single UserControl dynamically to represent the current page or if you want to do everything at design-time - a custom TabControl?
showed content from another winform
You generally don't embed a popup window into another, rather controls. Otherwise you have to deal with hiding Minimise, Maximise, Close etc.
MDI Form is the best solution for you.
Use: this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;`
to make your child Forms more beautiful in your project!
If you want the MDI parent to auto-size the child form you can code like this.
Code Example:
private void Form1_Load(object sender, EventArgs e)
{
IsMdiContainer = true;
}
private void btnHotel_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;
}
private void btnCaffe_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm3.MdiParent = this;
}
You can use TabControl control -> for each specific content you want to display you just have to add a TabPage to TabControl and when a specific menu option is selected, just switch to required TabControl page.
myTabControl.SelectedIndex = 1; // for selecting and displaying page with index 1
To hide navigation header of myTabControl, set those props (in constructor or in Form_Load event:
myTabControl.ItemSize = new Size(0, 1);
myTabControl.SizeMode = TabSizeMode.Fixed;
Then, you have only content of the TabControl page displayed, without navigation header.
First make it's this.IsMdiContainer = true; either from design time or run time.
then at the button's click event place the following code.
childForm frm = new childForm();
frm.MdiParent = this; //the current mdi parent form
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
frm.Show();
output
You can use mdi (multiple-document interface):
when you want a form start inside mainform, use this following code:
Dim hotelForm As New HotelForm()
HotelForm.MdiParent = Me // Me is your parent form want to open hotelForm inside
HotelForm.Show()
All forms have a property TopLevel, by setting this property to false, then you can deal with that form as a control, and add it to a panel control.
See below psuedo code:
Form2 newForm = new Form2();
newForm.TopLevel = false;
myPanel.Controls.Add(newForm);
newForm.Show();
After that, your main form design should look like as a navigation control and a panel beside it docked "Fill", then on clicking any navigation button, just create the desired form and set TopLevel to false, then show it on the panel
You have to put a Container control like a panel "pnlHost" in your form and use this for showing any form you want in it
private Form _currentForme;
private void ShowForm(Form frm)
{
_currentForme?.Close();
_currentForme = null;
_currentForme = frm;
_currentForme.TopLevel = false;
_currentForme.TopMost = false;
pnlHost.Controls.Clear();
_currentForme.FormBorderStyle = FormBorderStyle.None;
pnlHost.Controls.Add(_currentForme);
_currentForme.Dock = DockStyle.Fill;
_currentForme.Show();
}
I would use MDI parent and child for this.
But I guess you could also finish the child application, build the exe and initiate it as a new process from your Parent application.
Check the solution given here.
I tried a similar approach long ago and it worked perfectly.
Set current form as a MdiParent then use Datagridview to display data from other forms

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();

MDI parent size to fit MDI child form

I want the MDI parent to auto-size the child form so it fits inside without scroll bars. Can someone provide some code?
I have used this to differentiate the size of the parent and the child and add it to the size of the parent so I would get a fit. But it's so manual and takes too long to make.
void MDICentertoScreen(Form z,Size addedsize)
{
foreach (Form f in this.MdiChildren)
{
f.Close();
}
z.StartPosition = FormStartPosition.CenterScreen;
z.MdiParent = this;
// this.Size = Size.Add(z.Size, addedsize);
this.CenterToScreen();
z.Show();
}
Maybe this solve your problem:
form.MdiParent = this;
form.Dock=DockStyle.Fill;
form.Show();
Below is what I have used to solve this problem:
In the constructor of the child form, you add this line of code:
this.Dock = DockStyle.Fill;
In the Form_Load event, you should add this line of code:
this.WindowState = FormWindowState.Maximized;
That is, It solves my problem.
Good luck!
This is what I use:
Form form = new Form();
form.MdiParent = this;
form.Show()
form.WindowState = FormWindowState.Maximized;

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.

Categories

Resources