I have a C#, .net, Windows Forms application. I have a form set as an MDI container on which I dynamically add buttons. Clicking a button opens a child form. However, the buttons I created appear on top of the child form instead of the child form appearing over (and covering) everything on the parent form. What am I doing wrong?
Here's how I add the buttons to the form:
Button btn1 = new Button();
btn1.BackColor = ColorTranslator.FromHtml("#456EA4");
btn1.Text = department.DepartmentName;
btn1.Location = new System.Drawing.Point(posX, posY);
btn1.Size = new System.Drawing.Size(sizeX, sizeY);
btn1.Font = new System.Drawing.Font("Calibri", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
btn1.ForeColor = Color.WhiteSmoke;
btn1.TabStop = false;
this.Controls.Add(btn1);
Here's is where I open the child form:
frmToBuildFromSchedule frmToBuild = new frmToBuildFromSchedule(department);
frmToBuild.FormClosed += new FormClosedEventHandler(frmToBuildFromSchedule_FormClosed);
frmToBuild.MdiParent = this;
frmToBuild.Show();
Here is the result:
You are putting the buttons directly on the parent MDI form, which is not typical of an MDI style application. Instead, put the code that is currently in the click event of your buttons in a menu option or a ribbon button (those can also be dynamically created).
Alternatively, create a child form, maximize it, and place your buttons on that.
I think the answer by sam on the posted duplicate link is interesting. Rather than using MDI, set form.TopLevel = false; and add the form as a child control. I'm not sure if there are any downsides to this approach.
Form fff = new Form();
fff.Controls.Add(new Button { Text = "Button1" });
fff.Load += delegate {
Form ffff = new Form { TopLevel = false };
fff.Controls.Add(ffff);
ffff.Visible = true;
ffff.BringToFront();
};
Application.Run(fff);
I have a mainForm that has 3 panels. Panel3 is the one where I want my user control's to show. On my main form if I have a button (on left menu) I use this code and it works great:
panel3.Controls.Clear();
UserControl2 userControl2 = new UserControl2();
userControl2.Dock = DockStyle.Fill;
panel3.Controls.Add(userControl2);
My issue is when I have a button on that User Control 2 and I want that button click to bring up a UC called Employee List. It can't find panel 3. So my question is how can I tie Employee list to open up in Main Form Panel 3 or is there a better way to do that?
UPDATE: I did this and it works.. but it creates another main form with my employee list UC open now in panel3.. how can I just have it open and not create another main form?
mainForm mf = new mainForm();
mf.panel3.Controls.Clear();
employeeList empList = new employeeList();
empList.Dock = DockStyle.Fill;
mf.panel3.Controls.Add(empList);
mf.Show();
This piece of code did it!
mainForm f1 = (mainForm)Application.OpenForms["mainForm"];
f1.panel3.Controls.Clear();
employeeList empList = new employeeList();
empList.Dock = DockStyle.Fill;
f1.panel3.Controls.Add(empList);
f1.Show();
I have two forms; form 1 which contains the button button1 and MDIParent form.
By clicking the button1 I want to be redirected to the MDIparent form.
I am using the following code in click event of the button1
MDIParent f = new MDIParent();
f.ShowDialog();
However, the code gives no response when the button is being clicked.
You Must set Parent Form Property (IsMdiContainer = True)
MDIParent f = new MDIParent();
f.MdiParent = this;
f.Show();
excuse my english
well i have a Form MDI Conteiner and then i open a Child Form MDI like this
frSeries Serie = new frSeries();
Serie.Opener = this;
Serie.MdiParent = this;
Serie.Show();
seriesToolStripMenuItem.Enabled = false;
in the code above my Child Form receives the focus but when i select something from one combobox that i have in this new Form the background blink
but if i create the new form like this
frSeries Serie = new frSeries();
Serie.Opener = this;
Serie.MdiParent = this.MdiParent; //<----- this line is the problem
Serie.Show();
seriesToolStripMenuItem.Enabled = false;
the code above the Child Form don't receives the focus so i have to click in the child form and when i select something from one combobox that i have in this new Form the background not blink
i don't kown what is the correct form of open a MDI Form
I have tried as follows when clicking on a menu but the form still appears to be on the top left of the mdichild.I have also tried manual position but does not work.
Any suggestions?
Form1 form= new Form1{MdiParent = this};
form.StartPosition = FormStartPosition.CenterParent;
form.Show();
try
form.StartPosition = FormStartPosition.CenterScreen ;