I am having trouble with Windows Forms in C#, I used a dock in order to show several forms when they are selects in the menu on the top of the interface, and it is OK, it shows me the form I expected but there is a little detail: The most important part of the form, where are shown the buttons that will allow you save, edit or delete are cut off even when I set up the form to fill its dock in the function to show it (it is the one called abrirForm), so I need the dock showing the full form correctly. Below are the images and the relevant code.
How you can see, I am using enter image description heres
This is enter image description here
This is the method to show the form inside the dock (the one it is called "Contenedor", and also it changes the color of the option that is pressed:
private void abrirForm(IconMenuItem menu, Form formulario)
{
if(MenuActivo != null) {
MenuActivo.BackColor = Color.White;
}
menu.BackColor = Color.Silver;
MenuActivo = menu;
if(FormularioActivo != null)
{
FormularioActivo.Close();
}
FormularioActivo = formulario;
formulario.TopLevel = false;
formulario.FormBorderStyle = FormBorderStyle.None;
formulario.Dock = DockStyle.Fill;
formulario.BackColor = Color.PaleGreen;
Contenedor.Controls.Add(formulario);
formulario.Show();
}
This is the dock configuration in the designer.cs:
this.Contenedor.AllowDrop = true;
this.Contenedor.Cursor = System.Windows.Forms.Cursors.Default;
this.Contenedor.Dock = System.Windows.Forms.DockStyle.Fill;
this.Contenedor.Location = new System.Drawing.Point(0, 132);
this.Contenedor.Name = "Contenedor";
this.Contenedor.Size = new System.Drawing.Size(918, 503);
this.Contenedor.TabIndex = 3;
this.Contenedor.Paint += new System.Windows.Forms.PaintEventHandler(this.Contenedor_Paint);
And this is the result: The buttons are cut off from the window
In your case you can leverage the Controls Autosize property.
Since your child control formulario has not the size of the main Form because the extra space taken by the menu buttons what you need to do is look for a way to automatically increase the control added to the panel, fortunately there is a property Autosize as explained above and when you are adding a child control to a panel never set the child control dock to fill because it considers the main control size, so in order to fix your issue you need to do a few changes.
Remove this line:
formulario.Dock = DockStyle.Fill;
Set Autoscale for the main Form and the panel where you dinamically add controls, so add this:
this.AutoSize = true;
Contenedor.AutoSize = true;
So your abrirForm function will look:
private void abrirForm(IconMenuItem menu, Form formulario)
{
if(MenuActivo != null) {
MenuActivo.BackColor = Color.White;
}
menu.BackColor = Color.Silver;
MenuActivo = menu;
if(FormularioActivo != null)
{
FormularioActivo.Close();
}
FormularioActivo = formulario;
formulario.TopLevel = false;
formulario.FormBorderStyle = FormBorderStyle.None;
//formulario.Dock = DockStyle.Fill; I COMMENTED IT, YOU NEED TO REMOVE IT
formulario.BackColor = Color.PaleGreen;
//SET THE AUTOSIZE in Main form and Container
this.AutoSize = true;
Contenedor.AutoSize = true;
Contenedor.Controls.Add(formulario);
formulario.Show();
}
Please notice:
If above solution does not work is because the formulario code was not provided. Feel free to share it.