i'm developping a winforms application and i'm putting a mdi child form in splitcontainer.panel1.
when i want to close current mdi child to open another one i can't get the child form.
i'm using this code to open e new child but i want get the current child to close it :
Accueil accueil = new Accueil();
accueil.MdiParent = this;
accueil.TopLevel = false;
this.splitContainer1.Panel1.Controls.Add(accueil);
accueil.WindowState = FormWindowState.Maximized;
accueil.Size = this.splitContainer1.Panel1.ClientSize;
accueil.MinimizeBox = false;
accueil.MaximizeBox = false;
accueil.ControlBox = false;
accueil.Width = this.splitContainer1.Panel1.Width;
accueil.Height = this.splitContainer1.Panel1.Height;
accueil.Show();
Putting an MDI child window into a split container doesn't make any sense. You are turning the form into a plain control by setting its TopLevel property to false. Best not to lose the reference. But you will probably be ahead with:
while (splitContainer1.Panel1.Controls.Count > 0)
splitContainer1.Panel1.Controls[0].Dispose();
var accueil = new Accueil();
accueil.TopLevel = false;
accueil.Dock = DockStyle.Fill;
accueil.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
accueil.Visible = true;
this.splitContainer1.Panel1.Controls.Add(accueil);
Do consider using a UserControl instead, it is the sane approach with the least likely long-term confuzzlement.
Related
I am creating a C# .Net 4.8 Winform app with one MDI Form. This MDI Form has several panels and a split container.
When adding a childform I use the following code:
frm.Text = formTitleText;
frm.MdiParent = this;
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
panelDeskTop.Controls.Add(frm);
panelDeskTop.Tag = frm;
frm.BringToFront();
lblTitleChildForm.Text = $"{frm.Text}({this.MdiChildren.Length})";
frm.Show();
The form shows in the panel, but is not added as a MDI Child. The Parent (This) is the MDI Parent and has property of IsMDIContainer. So the this.MdiChildren.Length is always 0.
Not sure why?
Thanks in advance.
It's expected. You've set another parent for it: panelDeskTop.Controls.Add(frm);.
Consider the following points:
An MDI parent form has a control of type MdiClient. When you set MdiParent of a form, basically you are adding it to the controls collection of the MdiClient control.
When you ask MdiChildren of an MDI parent form, it returns the child forms of the MdiClient.
A form or a control, can have only one parent at time, and when you add it to controls collection of new parent, it will be removed from controls collection of the old parent.
Now I believe, it's clearer:
frm.MdiParent = this;
...
...
panelDeskTop.Controls.Add(frm);
The last line, removes the form from MdiChildren of the parent. That's why the array is empty.
Do you really need MDI?
It looks like you don't need MDI. If the child form is gonna fill the main panel without showing titlebar, then it basically means it doesn't need an mdi parent. Just make it a non-toplevel and add it to the controls collection of the main panel and show it.
Resizable sidebar for MDI parent
If you want have a resizable sidebar for MDI parent, then it's enough to Dock a panel and the splitter to left of the parenr, and then the right side will be occupied by the MdiClient. For example:
var mdiParent = new Form() { Size = new Size(700, 450), Text = "parent" };
mdiParent.Load += (obj, args) =>
{
mdiParent.IsMdiContainer = true;
mdiParent.Controls.Add(new Splitter()
{ Dock = DockStyle.Left, BackColor = Color.White, Width = 8 });
mdiParent.Controls.Add(new Panel()
{ Dock = DockStyle.Left, BackColor = Color.Black });
var child1 = new Form()
{ MdiParent = mdiParent, Text = "child1", Size = new Size(400, 300) };
var child2 = new Form()
{ MdiParent = mdiParent, Text = "child2", Size = new Size(400, 300) };
child1.Show();
child2.Show();
};
mdiParent.ShowDialog();
I'm trying to add a form to a SplitContainer from a child form. I can do it from the forms parent using this.
Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = this;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
this.splitContainer.Panel2.Controls.Add(form);
form.Show();
But I can't figure out how to do it from a child of the parent form.
Any help is appreciated.
Here's how I solved the problem. I passed a reference to the child form.
MessageBoxRegister register = new MessageBoxRegister(this);
register.ShowDialog();
I then saved the reference in a global variable.
Launcher launcher;
public MessageBoxRegister(Launcher launcher)
{
InitializeComponent();
this.launcher = launcher;
}
Then I could open the form into the splitContainer like this.
Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = launcher;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
launcher.splitContainer.Panel2.Controls.Add(form);
form.Show();
I want to combine several winform applications into 1 master application. I created a new winform project/solution and added the forms from two of the solutions. I added TabPages to the first form from which to launch other forms. I also changed the project Output Type to Library as recommended in combine multiple C# projects I am having difficulty getting the 2nd form (classBuilder) to load when I click the Class Builder tab.
I Googled how to add a form to a TabPage and most posts had very similar code that I inserted as shown but nothing happens when I click the tab. As a novice I have no idea if I’m on the right path or placed the subject code in the right place. Need someone way smarter than me to get me on track. A few concerns I have are: 1) Each form has a unique app.config file and if I attempt to rename it the main form doesn’t fill in so I put it back to App.config. I imported the config file for the 2nd form and renamed it ClassBuild.config and the concern is the 2nd form won’t fill in if and when I get it running with some expert help. 2) I don’t understand why I would issue ‘Form frmClassBuilder = new Form();’ when a form by that name already exists. Is this code okay?
namespace VX130
{
public partial class VX130UI : Form
{
public DataTable tblPKIEN;
public DataTable tblsAttributes;
public DataTable tbltAttributes;
public DataSet dsVX130;
SqlDataAdapter da = new SqlDataAdapter();
public VX130UI()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
//attempt to add form to a tabpage
Form frmClassBuilder = new Form();
frmClassBuilder.TopLevel = false;
tabPage9.Controls.Add(frmClassBuilder);
frmClassBuilder.Parent = this;
frmClassBuilder.WindowState = FormWindowState.Maximized;
frmClassBuilder.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmClassBuilder.Dock = DockStyle.Fill;
frmClassBuilder.Show();
//end attempt
// tabControl1
//
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage8);
this.tabControl1.Controls.Add(this.tabPage9);
this.tabControl1.Controls.Add(this.tabPage10);
this.tabControl1.Controls.Add(this.tabPage11);
this.tabControl1.Controls.Add(this.tabPage12);
this.tabControl1.Location = new System.Drawing.Point(13, 65);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(963, 445);
this.tabControl1.TabIndex = 2;
// tabPage9
//
this.tabPage9.Location = new System.Drawing.Point(4, 22);
this.tabPage9.Name = "tabPage9";
this.tabPage9.Padding = new System.Windows.Forms.Padding(3);
this.tabPage9.Size = new System.Drawing.Size(955, 419);
this.tabPage9.TabIndex = 3;
this.tabPage9.Text = "Class Builder";
this.tabPage9.UseVisualStyleBackColor = true;
this.tabPage9.Controls.Add(ClassBuilderUI.frmClassBuilder()); is a ‘type’ and not valid in given context
I found what was missing piece to create instance of an existing form (exists in solution).
public VX130UI()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
ClassBuilderUI.frmClassBuilder frmClassBuilder = new ClassBuilderUI.frmClassBuilder(); <== to reference existing form (exists in solution
//Form frmClassBuilder = new Form();
frmClassBuilder.Dock = DockStyle.Fill;
frmClassBuilder.TopLevel = false;
frmClassBuilder.Visible = true;
tabPage9.Controls.Add(frmClassBuilder);
tabPage9.Show();
Show the TabPage, not the form. And don't set things like Parent and WindowState for the form you are adding:
//Form frmClassBuilder = new Form();
ClassBuilderUI.frmClassBuilder frmClassBuilder = new ClassBuilderUI.frmClassBuilder(); <== to reference existing form (exists in solution
frmClassBuilder.Dock = DockStyle.Fill;
frmClassBuilder.TopLevel = false;
frmClassBuilder.Visible = true;
tabPage9.Controls.Add(frmClassBuilder);
tabPage9.Show();
I don’t understand why I would issue ‘Form frmClassBuilder = new
Form();’ when a form by that name already exists.
Exists where? How? You need an instance of the form to add to the Tab page control collection. If you already had an instance, you could add it. Just be sure it doesn't get Close() or Dispose() called on it.
The code in your ClassBuilderTab_MouseClick handler looks useless. I would remove that unless you have some other need for it.
I Create a Windows Forms application with C#.
I have a general Form and a panel on it.
I show subForm into this panel with code:
SubForm objForm= SubForm.InstanceForm();
this.IsMdiContainer = true;
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
now I want to show other form on subForm of this panel, But I dont know how to do it.
I think your problem resolved by this code:
SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
As I understand, you're very close. To add another form into subform try the same code instead:
pnlSubSystem.Controls.Add(objForm);
use (where objForm2 is the new subForm)
SubForm objForm2 = new SubForm();
objForm.Controls.Add(objForm2);
Since you already got the answer that by removing this.IsMdiContainer = true; your code would run perfectly fine. Because IsMdiContainer property changes the display and behavior of the form to an MDI parent form. When this property is set to true, the form displays a submerged client area. All MDI child forms assigned to the parent form are displayed within its client area.
SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
objForm form which will be the template for the child forms. Each time you want to create a new child window to your application, you can create a new instance of this template form and make the first form as its parent form.
//Create a new instance of the MDI child template form
SubForm objForm = new SubForm();
//Set parent form for the child window
objForm.MdiParent=this; // Last ObjForm or something
//Display the child window
objForm.Show();
Another way:
objForm.TopLevel = false;
objForm.Parent = pnlSubSystem;
objForm.Show();
This is my first answer on Stackoverflow.
I am making an application in C#, and it has a menu, having forms linked with it, i want
that there should be a parent form, having a panel or window, when we click on any menu link, its .cs form should be loaded in the window, and so we can click on other windows, and their forms should replace the current one. Just like a common windows software.
Regards
Touseef Khan
Your Form(Window) must be MDI
YourForm.IsMdiContainer = True
NewForm.MdiParent = YourForm;
NewForm.Show();
Try this:
var f2 = new Form2();
f2.TopLevel = false;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Size = this.Size;
f2.BringToFront();
f2.Visible = true;
this.Controls.Add(f2);