Passing data from MDI Child to MDI Parent - c#

I new some help to pass data from my mdi child to the mdi parent.
And I want to show the string (on the mdi parent) that I wrote in the mdi child.
I also have to make the string appear after close the mdi child.
Any help?

From the parent form you can access the parent form my name
Form frm = (Form)Application.OpenForms["ParentFormName"];

Just declare a variable in parent with internal access and assign values of child form to that variable. You should be good to use the value on parent form.

Related

Can I show the ToolStrip of a child Form in the MDIParent Form?

I have an MDI Form and I want to merge/add the child Form's ToolStrip with parent Mdi Form.
Normally, a child Form's ToolStrip is shown only inside the owner child Form, but I want to merge it with the MdiParent's.
I can do it with the MenuStrip but hardly find the ToolStrip to do the same.
The ToolStripManager.Merge() method can be used to merge the MdiParent's ToolStrip and a child Form's Toolstrip. The child Form's ToolStrip will be moved to the MdiParent's ToolStrip.
The ToolStripManager.RevertMerge() method will remove the child Form's ToolStrip when the child Form is closed.
Of course, use the real names you have assigned to MdiParent ToolStrip (here, MdiToolStrip) and the child Form ToolStrip (here, named toolStrip1) and adapt the Forms names to what you're using.
Assign distinct names to the child Forms ToolStrip controls.
When you create a child Form instance, add this before showing it:
var child1 = new ChildForm1();
child1.MdiParent = this;
ToolStripManager.Merge(child1.toolStrip1, this.MdiToolStrip);
child1.toolStrip1.Visible = false;
child1.FormClosed += (s, ev) => { ToolStripManager.RevertMerge(this.MdiToolStrip, child1.toolStrip1); };
child1.Show();
If you have a MenuStrip in the MdiParent and in your child Forms, the ToolStripMenusItems will be merged automatically, but I suggest you add this right after InitializeComponent() in the MdiParent Constructor:
this.MainMenuStrip = MdiMenuStrip;
where MdiMenuStrip is the name assigned to the MdiParent's MenuStrip.
The reason is explained here:
How to avoid screen bouncing when adding a new MDI Child Window
Further readings (MSDN Docs):
How to: Set Up Automatic Menu Merging for MDI Applications
Merging Menu Items in the Windows Forms MenuStrip Control

Whats the difference between Parentform and Owner

In winforms we have objForm.Owner and objForm.Parent. Whats the difference between these two.
I opened a form B from form A as a dialog and was expecting to access form A's public properties from form B using ParentForm property but finally ended up using Owner property instead as ParentForm was null !!
A parent-child relationship exists between windows when the child is embedded in the parent window and cannot move outside of its bounds. Examples are child controls like TextBox and Panel. And the MDI windowing model, MDI child windows are embedded in the MDI parent and parented to the dark-gray MDI client window.
An owned window applies to top-level windows and primarily controls their Z-order. An owned window is always on top of its owner. It is also minimized and restored along with its owner. Examples are tool windows and dialogs.
Note how a Form is normally a top-level window and does not have a parent. So wouldn't have a use for its Parent and ParentForm properties. It can however be turned into a child window by setting its TopLevel property to false. Sample code is here.
Form.Owner - Is the Form that "owns" this form. For example Find/Replace dialog would be Owned by Notepad's main window. If you
minimize the main Form, the owned form will minimize, if you restore
the main form, the owned form will restore
ContainerControl.ParentForm - Is the Form that this ContainerControl is ultimately placed on
Check this article. Their is explained Parent too.

Closing all the instances of MDI child forms and the parent form in C#.net?

IDE: Microsoft Visual Studio 2010
Platform : C#.net
I have Child forms being loaded on the MDI parent form. So, now i want to close the MDI parent and all its child form when I click on the finish button which is in the last child form. And once i click on the finish button it should close all the instances of parent and child form and open another form which is not part of the MDI parent form ?
You could try to enumerate forms in Application.OpenForms, check their relations and close what you need:
// close childs of parentForm
foreach(var form in Application.OpenForms)
if(form.MDIParent == parentForm)
form.Close();
Or you can register childs when they are created, so you will have a list of childs to close (pseudo-code):
// container for childs
List<Form> _childs = new List<Form>();
// when adding new form
Form1 form1 = new Form1();
form1.MDIParent = parentForm;
// register it
_childs.Add(form1);
// unregister when child is closed
form.OnClosed += ChildClosed;
// in ChildClosed
// unregiser
_childs.Remove(sender as Form);
It's just a robust idea. Childs registering could be optimized (by having base class for childs which does all that automatically). Registering childs is better if you going to have some extra data for each childs, to example, store they location and size for layouting.

parent form is accessible when child form is open

I have created a parent form and a child form in c# when i click a menu item in parent form, it opens the child form but still i access the parent form.i want that the parent form will remain inaccesible until the child form is open.please send me the code.thnks
try with this
form.ShowDialog()
Probably you should use form.ShowDialog() method instead of form.Show()
Definitely you should add a better description, at least framework you are using (WinForms?). C# is not a framework.
Use, form.Hide() process to make the Parent form to be not accessable when ever the child form opens and again give form.show() to activate the parent form.
If you want your parent form visible while showing your child form then you can do following.
form.ShowDialog(this).
Where this is the instance of your parent form.
On the other hand, if you want your parent form hide while showing child form you can do following
this.Hide();
form.ShowDialog(this)

How to make all forms in a project as mdi child of mdi form.

In my winforms I have mdi form. Here I want to make all forms as child of mdi parent.
My forms structure are like this.
MainForm (Mdi parent)
FrmEmployeeDetails(child of MainForm)
FrmNewEmployeeDetails(child of FrmEmployeeDetails)
When I execute the project.form NewEmployeeDetails is opening outside the MainForm.
For FrmEmployeeDetails I coded like this.
FrmEmployeeDetails EmployeeDetails= new FrmEmployeeDetails();
EmployeeDetails.MDIparent=this;
EmployeeDetails.show();
FrmNewEmployeeDetails is child form of FrmEmployeeDetails.
For the form FrmEmployeeDetails I am creating NewEmployeeDetails as like this.
FrmNewEmployeeDetails newemployeedetails = new FrmNewEmployeeDetails();
newemployeedetails.show();
How can I make FrmNewEmployeeDetails
as mdi child of MainForm.
Thanks in advance.
you should make this
FrmNewEmployeeDetails newemployeedetails = new FrmNewEmployeeDetails();
// Set the Parent Form of the Child window.
newemployeedetails.MdiParent = this;
// Display the new form.
newemployeedetails.Show();
Change the line
EmployeeDetails.MDIparent=this;
to
EmployeeDetails.MDIparent=this.MdiParent;

Categories

Resources