Open ChildForm with owner permission - c#

I am trying to insert a form inside another form. I used .Controls.Add(form) and it works. My problem is that I have to declare the owner and to do that I have to set the toplevel to true but if I set the toplevel to true I get stuck with the control.add bacause telling me that it is impossible to add a top level control to a control.
How can I do?
public void openChildForm(Form childForm)
{
if (activeForm != null) activeForm.Close();
activeForm = childForm;
childForm.TopLevel = true;
childForm.TopMost = true;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
this.Controls.Add(childForm);
panelChildForm.Tag = childForm;
childForm.Owner = this;
childForm.BringToFront();
childForm.Show();
}
thankyou in advance,
Davide.

For me it's a bit strange what you are trying to do. A form is kind of window. Why would you add a window in another window? Don't you want to create a user control and to display that user control in your existing form?

There isn't enough information to go on from the original question you've posted, but it looks like you're trying to create the kind of functionality that a sidebar page menu might offer. In that situation, clicking on a button on the sidebar changes the window that's currently being displayed. Perhaps this question about sidebar navigation would relate to what you're saying? There's some information also available if you try searching "winforms sidebar navigation," such as this post about creating navigation in winforms, and there are a lot more linked from this question.
I'm just taking a guess at what you're trying to do here. Perhaps if you want to refine the answers you're getting, the original question should include a link to a github sample project, or at least a bit more of an explanation of what the target is.

It looks like you are working with WinForms.
Look at MDI Forms (Multiple-Document Interface Forms)
You can handle this by setting the parent form property IsMdiContainer = true
and then the child form childForm.MdiParent = parentForm;.
Here is a small example:
Add a menuStrip to the parentForm and set the parent form MainMenuStrip = menuStrip. Add a menu item and add some code to the menuStrip_ItemClicked event.
private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "New Child")
ShowNewForm(sender, e);
}
private void ShowNewForm(object sender, EventArgs e)
{
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "Window " + childFormNumber++;
childForm.Show();
}
You should end up with this behaviour.
If you want to access other forms within the same application you can use a loop and find a form using Application.OpenForms or find a form using either Application.OpenForms["form1"] or Application.OpenForms[0]
If you use MdiForms you can find child forms using parentMdiForm.MdiChildren

Related

How to create a windows form that replaces the current form?

I am making a game using Windows Forms, and I feel that the easiest way to make different screens (Main Menu, Settings, Etc...) is using a separate form for each screen. I have looked up up many different ways to do this. Many people talk of using form2.Show(); this.Hide();, however, this creates a new popup window.
People have also suggested using this.IsMdiContainer = true; to create a new window inside of the current window, but this is also not the functionality I want.
EDIT: Another question, multiple-guis-one-window, explains slightly that this can be achieved using UserControls, but does not elaborate with any examples, or what I should do.
I want to completely replace the current form's functionality with that of a new form.
Is there a way to achieve this? If not, is there a good alternative?
As I understood, you want to keep the same form and change the data inside that form (to not get the effect that your form was closed and reopened again).
The problem is windows form does not support such thing directly (at least not in an optimal way), I would use another UI frameworks for that. However, if you want to stick though with windows forms, you may use GroupBox or Panel tools (available from windows form design tools in Visual studio). Here you can group your elements as required, and show/hide the group/panel as required.
That is one of the best available solution for windows form AFAIK.
You want to open child forms within main form then you should try this i have create it without any User Control.
I have manage one parent form and two child forms. child forms should be open within parent form.
frmMain(Parent Form)
Set frmMain Property as Following
WindowState = System.Windows.Forms.FormWindowState.Maximized
I have take 3 panel in frmMain window.
pnlMenu (For Display Menu)
Set pnlMenu.Dock = System.Windows.Forms.DockStyle.Top property
Set height of this panel as you require.
pnlMain (For Display Child Forms)
Set pnlMain.Dock = System.Windows.Forms.DockStyle.Fill property
pnlFooter (For Footer Section)
Set pnlFooter.Dock = System.Windows.Forms.DockStyle.Bottom; property
Set height of this panel as you require.
I have set menubar in pnlMenu(Click on that menu for display child form in pnlMain)
frmMain.cs
public void BindFormIntoMainForm(Form Main)
{
Main.TopLevel = false;
Main.WindowState = FormWindowState.Maximized;
Main.AutoScroll = true;
pnlMain.Controls.Clear();
pnlMain.Controls.Add(Main);
pnlMain.Refresh();
Main.Show();
}
private void childToolStripMenuItem_Click(object sender, EventArgs e)
{
frmChildForm1 ChildForm1 = new frmChildForm1();
BindFormIntoMainForm(ChildForm1);
}
private void childForm2ToolStripMenuItem1_Click(object sender, EventArgs e)
{
frmChildForm2 ChildForm2 = new frmChildForm2();
BindFormIntoMainForm(ChildForm2);
}
BindFormIntoMainForm method responsible for display child form in main window.
frmChildForm1 & frmChildForm2(ChildForm)
Set both form Property as Following
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Now when you click on Child Form 1 Menu then display following output:
when you click on Child Form 2 Menu then display following output:
I think it can be helpful for you.

Form disappears when resetting Form's TopLevel property

In my application I am displaying a new form which needs to be at TopLevel.
So, I am setting
someForm.TopLevel = true;
Now, I have a checkbox, which will allow user to set it to "not a top level".
When unchecked, i want to set TopLevel = false
But when I do this, my form disappears. Does anyone know why?
Here is my code:
private void stayOnTop_CheckedChanged(object sender, EventArgs e)
{
this.TopLevel = this.stayOnTop.Checked;
}
Because your checkbox is named stayOnTop, I assume you want to set the TopMost property instead of TopLevel.
The setting TopLevel is only meaningful in MDI applications - ones where a parent form has one or more child forms inside it (like Word and Excel used to work).
TopLevelControl is the main form of your application. By setting TopLevel to false the TopLevelControl is set to null. In that case there is no main form to display for your application. If you add a timer that will switch it back to true you will see that it appears again. (It is interesting though that there is no preventing mechanism. For instance it's not possible to add a top level form to another top level form. But you are allowed to get rid of top levels forms entirely.)
So that's why it disappears. If you want it only sent to background you can use SendToBack() method. It will change Z-index of the form. So if there is a window behind your control, the control will be moved behind the window.
Try these codes :
private void stayOnTop_CheckedChanged(object sender, EventArgs e)
{
if(e.checked == true)
{
someForm.TopLevel = false;
}
else
{
someForm.TopLevel = true;
}
}

Show a Winform 'Form' as if it were a usercontrol, within another form

Edit
The answer to This Question, though an obvious solution, is insufficient for this case. I've bolded the reason in my original question explaining why I wanted it 'literally answered' - thanks LarsTech!
My Original Question:
I have a library with 100 WinForms Form screens in it that is 'Beyond My Control' and there are a few I can share in my app. However, my app uses a tab control and I need the content of the forms to appear within the tabs instead of as popups.
When I try to new up one of the forms and add it to the controls collection of a tab, I get this error message:
Top-level control cannot be added to a control.
Anybody know how to get around this? I know there are other ways involving better architecture, but deadlines, backlogs, and rich new customers await. [sigh/]
private void button1_Click(object sender, EventArgs e)
{
//panel1.Controls.Add(new Button());
Form f = new Form();
f.Controls.Add(new CheckBox());
//f.ShowDialog();
panel1.Controls.Add(f);
}
Try turning the TopLevel off:
f.TopLevel = false;
f.FormBorderStyle = FormBorderStyle.None;
f.Visiible = true;
Now it is essentially a UserControl. A word of caution though, since it's a form, it will show up in the Application.OpenForms collection, which is probably not your intention.
You need to set the form's TopLevel property to false, like this:
f.TopLevel = false;
You may also want to set the FormBorderStyle property to None.
f.FormBorderStyle = FormBorderStyle.None;
As mentioned in the comments above, it really would be best, when possible, to make it a UserControl.

control box showing twice [duplicate]

All, I have a WinForms MDI control and in it I dock several child windows. When I first did this I managed (somehow) to get rid of the window list (shown above the tabbed forms below)
I am not talking about the double window menu (on the right) I know that this is due to a bug in the WinForms control and that if you add MdiChild elements in the Load event instead of the Constructor, this behaviour resolves itsef (see this post for details).
Here I am talking about the menu strip itself, I don't want it! How do I get rid of it? Any advice is much appreciated...
Note: I am adding MdiChild forms in the following way:
foreach (Form mdiChild in MdiChildForms)
{
mdiChild.MdiParent = this;
mdiChild.Show();
}
where MdiChildForms is a List<Form>.
Here is the possible solution:
public MainForm() {
IsMdiContainer = true;
InitializeComponent();
this.MainMenuStrip = new MenuStrip(); // create our own menu strip
this.MainMenuStrip.Visible = false;
}

Updating Form1's widgets by clicking Form2's button in Visual C# Windows Forms

I'm fairly new to Visual C# and I'm writing an GUI app with multiple forms. One form is main window, and the rest are some kind of option windows. When showing an option window, I need to load some data to it (for example a string to window's editbox), then edit it and return back to main window when closing option window. Is there any simple way I can achieve it?
I've found some solutions like, or c# event handling between two forms, but I can't really conform it to my needs. I was thinking about passing data in constructor, but how to get it back? I've found something about ShowDialog, but as I said I'm new to C# (started yesterday ^^) and don't know if I can use it.
Any ideas, please?
I found the following previous answer which outlines sending specific properties from the one form to another:
Send values from one form to another form
The using keyword will also ensure that the form is cleaned-up properly, here's a link to it's usage (pardon the pun...) : http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx
I've run into the same issue to be honest, and I have to say that prior to this discussion I would just pass the parent form itself to the child and alter it in that way. Such as:
ChildForm child = new ChildForm(this); //from the parent
and
public ChildForm(ParentForm parent)
{
this.parent = parent;
}
Probably not the best convention though, as you probably don't need to access that much from the parent as the child.
Thanks guys, I think I finally get it. Idle_Mind, your idea was the easiest in my point of view, so I decided to use it. If someone else has a problem like this, here's what I've coded:
In main window form: when button is clicked, a new form appears; after closing it, label1 shows the text typed in that form
private void Button1_Click(object sender, EventArgs e)
{
LoadDataForm loaddata = new LoadDataForm("initial value");
if (loaddata.ShowDialog() == DialogResult.OK)
{
label1.Text = loaddata.textBox1.Text;
}
}
In load data form: argument passed in form's constructor appears in textBox1; textBox1's Modifiers property has to be modified to "public"
public LoadDataForm(string initvalue)
{
InitializeComponent();
textBox1.Text = initvalue;
}
private void ApplyButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Regards,
mopsiok

Categories