Remove top bar on child form - c#

Making an MDI form, i wish to remove the top bar on all child forms. Working on Visual studio and c#. Any idea how? Im clueless.
Here the properies of the child form:
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Normal
Me.MinimizeBox = False
Me.MaximizeBox = False
Me.ControlBox = False
Me.ShowIcon = False
Me.ShowInTaskbar = False
Me.Dock = DockStyle.Fill

Option 1: *
As mentioned in this answer, you can add a MenuStrip control to your MDI parent form, set its Visible property to false, and you should be good to go. The MDI child forms won't have a title bar displayed as long as they are maximized.
Option 2: *
Set the MdiParent Property of the child form.
Set the FormBorderStyle property of the child form to FormBorderStyle.None.
Set the Dock property of the child form to DockStyle.Fill. Note: This must come after setting the MdiParent or else it won't work.
That's it, you don't need to change any other properties (WindowState, ControlBox, etc.). Just maintain the order of the steps above.
Here's an example:
private void OpenAndDockMdiChild()
{
Form2 childForm = new Form2();
childForm.MdiParent = this; // This must come **before** setting
// the `Dock` property.
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
childForm.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
OpenAndDockMdiChild();
}
Result:
Hope that helps.
* Tested with .NET 4.5.2 on both Windows 7 and Windows 10.

Related

C# Winform MDI Form with Split Container - MdiChildren is empty

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

Parent form's control to be in front of child form (Winforms)

I have a program with a horizontal dropbox with 4 options to choose from in which "sub buttons" will appear when I click on one of these buttons:
Here is the interface:
And what I want to do is that when I click on one of the drop down menu options, I want a childform to spawn on the black area, using this:
private Form activeForm = null;
private void openChildForm(Form childForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
MainInterfacePanel.Controls.Add(childForm);
MainInterfacePanel.Tag = childForm;
childForm.BringToFront();
childForm.Show();
}
However, when I do so, the childform does appear normally, but it covers the dropdown menu, preventing me to select other functions:
Here's how it looks like(the childform is still a blank form, thus the whiteness):
I tried solving this issue by using item.bringToFront() on the buttons, but it dosent seem to work.
Basically, I want the Parent Form to stay behind the childform, while having the buttons be infront of the childform, like an overlap. Is there a way to do so?
Also, when I remove the 'childForm.BringToFront()', for whatever reason it spawns behind the interface.
You should have to use Panel in which child form may appear. Simply add Panel from Toolbox and change its Dock property to top and place all your menu items in that Panel and also add new Panel i.e. MainPanel to display forms and than use following code to display form inside the Panel
SubForm objForm=new SubForm();
objForm.TopLevel = false;
MainPanel.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();

Setting formBorderStyle to None fails with error FormBorderStyle is used as a variable

System.Windows.Forms.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = System.Windows.WindowState.Maximized;
I am trying to set the FormBorderStyle to None (to get fullscreen) but I I always get an error that the
FormBorderStyle is used as a variable.
See this line of code that you have. This line works because it is saying that the WindowState is assigned the value on the right.
WindowState = System.Windows.WindowState.Maximized;
Then you have this line of code:
System.Windows.Forms.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
That is similar to doing something like this:
int = int; // will not work
Both on the left and right side you have a type. You need to change it to this:
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Now you are saying that FormBorderStyle of current window is the value on the right.
EDIT
In WPF you will do it like this:
this.WindowStyle = WindowStyle.None;
You are trying to set type to one of its possible values, so it's not going to work.
You need to assign the Form.FormBorderStyle property on the instance of that form in some event handler or inside its constructor.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}
...
Also you can probably set this property through the designer if your application never has to have another border style.

Place form1(No border style) inside of form2(Normal border style) [duplicate]

This question already has an answer here:
Add Form to a UserControl - is this possible?
(1 answer)
Closed 8 years ago.
I would like to know if it is possible to show/place a form1 INSIDE form2.
So that form1 is stuck inside form2.
Any help will be appreciated. thanks in advance.
Yes. The word you're looking for is MDI or Multiple Document Interface.
Just set form2's IsMdiContainer property to true
and set form1's MdiParent property to be form2 like so:
form2.IsMdiContainer = true;
form1.MdiParent = form2;
Of course, you cause the visual designer to write the first line for you by setting the property in the properties section:
EDIT
Here's an easy example.
Say you have 2 forms. FormContainer and FormChild.
FormContainer is the application's main form.
All you need to do is make sure that FormContainer's IsMdiContainer property is set to true and then you could add instances of other forms to this one by setting those instances' MdiParent property. Except for the main form, any instance of the Form class or a subclass is by default not visible.
public partial class FormContainer : Form {
public FormContainer() {
InitializeComponent();
this.IsMdiContainer = true;
// if you're not excited about the new form's backcolor
// just change it back to the original one like so
// note: The dark gray color which is shown on the container form
// is not it's actual color but rather a misterious' control's color.
// When you turn a plain ol' form into an MDIContainer
// you're actually adding an MDIClient on top of it
var theMdiClient = this.Controls
.OfType<Control>()
.Where(x => x is MdiClient)
.First();
theMdiClient.BackColor = SystemColors.Control;
}
private void FormContainer_Load(object sender, EventArgs e) {
var child = new FormChild();
child.MdiParent = this;
child.Show();
// if you wish to specify the position, size, Anchor or Dock styles
// of the newly created child form you can, like you would normally do
// for any control
child.Location = new Point(50, 50);
child.Size = new Size(100, 100);
child.Anchor = AnchorStyles.Top | AnchorStyles.Right;
}
}

How to Load Form inside panel other form in win app

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.

Categories