Well this maybe is simple but I cannot nail it.
I have a win forms application with a from as Mdi Container with n mdi childs.
How to set ActiveMdiChild ?
For example i have 4 mdi childs (0,1,2,3) and i want to pass the appropriate data to each one...but only the active gets the data..
To activate a specific MDI child, you can use ActivateMdiChild method of the MDI parent and pass the child to activate. For example:
this.ActivateMdiChild(this.MdiChildren[0]);
ActivateMdiChild (Form)
Activates the MDI child of a form.
If the form parameter is already the active MDI child form, then the
ActivateMdiChild method simply returns. If form is not null, then it
is set to be the active MDI child form. Whether the form parameter is
null or not, ActivateMdiChild raises the MdiChildActivate event.
You can do this:
foreach (Form frm in this.MdiChildren)
{
if (frm is Form1)
{
Form frmChild = this.ActiveMdiChild;
frmChild .Show();
}
}
If the child have different form names, you can just have if statement to check if this is the form you are checking then set it as your acitvemdichild.
Related
Is it possible to control the parent form from a modal child form?
My only idea is to close the parent form and show child form via ChildForm.Show() and not ChildForm.ShowDialog(parentform), can I control parent form from modally displayed child form?
How to refresh a parent form automatically upon saving the update on the child form?
You can access the parent (Owner) form by using Owner property. If you show the dialog using:
childForm.ShowDialog(this);
(this will be the form from where you are showing the dialog)
then in the dialog:
this.Owner.BackColor=System.Drawing.Color.Coral;
This will change the background colour of the parent form immediately.
ParentForm is available in the context of MDIForm context.
I have shown a new form in button click on parent form like below,
Here the background parent form gets disabled when a new child form is activated.
Is there any available options to show the child form without disabling the parent form?
Regards,
The Show function shows the form in a non modal form. This means that you can click on the parent form.
ShowDialog shows the form modally, meaning you cannot go to the parent form
Application.Run() runs the main parent form, and makes that form the main form. Application.Run() is usually found in main.
If it is fully disabled (no interaction possible), you are using .ShowDialog() on the child form instead of .Show(). If you use .Show() you will be able to use both forms
Example:
ChildForm childForm = new ChildForm();
childForm.Show();
after you open the new form and call this.Activate(); it will refocus on the parent window, but this will cause it to loose focus for a fraction of a second
I have created one MDI form & Want to inherit a already made Child form. So what I have to do to access all controls on my MDI in that child form. (I dont want to create a new inherited form, I have already one)
Your question is rather unclear but it appears that you are asking how to show a form as an MDI child. I assume that you have already set IsMdiContainer to true for your main form.
In order to show an MDI child you need to set the MdiParent property for the child form. Like this:
ChildForm myChild = new ChildForm();
myChild.MdiParent = this;//this is the main form, the MDI parent
myChild.Show();
I have a simple problem: I have a main form in win-forms/c#. It has a listbox bound to a database.
When I click a button a new form is created.
When I click a button on the child form, I want to call a method that exists in the main form, that updates the list box or alternatively when the child form closes, to call that function.
Is this possible??
There are many ways to achieve this, but here's a simple way. In your main form, when you create and show a child form, do it like this:
ChildForm child = new ChildForm();
child.Show(this); // this calls the override that takes Owner parameter
Then, when you need to call a method in the main form from the child form, use code like this (assumes your main form is of type MainForm):
MainForm parent = (MainForm)this.Owner;
parent.CallCustomMethod();
A more complex way would be to use a form of dependency injection, where you would pass in a reference to the parent form (or more properly, to its interface) in the constructor of the child form. But the above way is simple and probably effective enough for your purposes (and it actually is a form of dependency injection itself, sort of).
Scenario 1: Call a method in Parent Form on click of button in child form.
Create an Event in Child Form. Raise that event on some Button Click etc. Subscribe to that event in your Parent Form and call the parent's form method inside that.
Scenario 2: Call a method in Parent Form when Child Form is closed.
Handle the FormClosed or FormClosing event of Child Form in the Parent form and call the parent's form method inside that.
ChildForm frm = new ChildForm();
frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
void frm_FormClosed(object sender, FormClosedEventArgs e)
{
//Call your method here.
}
I have a little application that creates alerts whenever a change to a database is made.
I have a few options in the alert form that pops up.
One of the options opens another form (a child form) asking the user for further information.
When the child form gets the necessary information from the user, I want it to close as well as the parent form. So far, I only know how to close the child form, but not the parent form.
Parent form > Opens child form
Child gathers information > User clicks ok in child > child closes, parent closes
^this is what I want
I just don't have the brain power to think about how to communicate across forms to accomplish closing the parent form.
Any help would be much appreciated. Actually, it would be super appreciated. If I could learn how to make my forms communicate with each other, I could really do a lot of damage (in a good way 8D ).
In the parent form, you can do something like this:
ChildForm f = new ChildForm();
f.FormClosed += (o,e) => this.Close();
f.Show();
Try this in the parent form:
using (var childForm = new ChildForm())
{
if (childForm.ShowDialog() == DialogResult.OK)
{
Close();
}
}
Your child form should return a DialogResult by clicking buttons (OK or Cancel) and/or setting the AcceptButton and CancelButton properties in the designer.