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();
Related
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 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();
I have a couple of forms, and I want to be able to add a button to a certain form, no matter where the code is.
Normally I'd do something like this.Controls.Add(button), but I don't want it to be added to that form. I've tried doing something like Form1 frm = new Form1() and frm.Controls.Add(button), But that didn't work either. How do I need to write it?
This code doesn't work, the form is still blank
Button b = new Button();
b.Size = new Size(50,50);
b.Location = new Point(50,50);
new Form1().Controls.Add(b);
There are no errors, but nothing is added.
I have found a kind of work around.
Control ctrl = this;
ctrl.Controls.Add(b);
This works, But I'd rather have a way to specify exactly which form to add it to
Button b = new Button();
b.Size = new Size(50,50);
b.Location = new Point(50,50);
new Form1().Controls.Add(b); // This will do nothing you want.
Adding control to a form will add it to the single instance on which you run it, not to the form in general.
Try this:
Form1 form = new Form1();
Button b = new Button();
...
form.Controls.Add(b);
form.ShowDialog(); // Or .Show()
Form1 anotherForm = new Form1();
anotherForm.ShowDialog(); // This instance will NOT have the added button
If you're doing this from another form, you can also try this:
// Constructor
this._otherForm = new Form1(); // save reference of the other form, to be able to add controls to it later
// Anywhere in the code
this._otherForm.Show(); // will display the other form
// On user action, for example on button click
this._otherForm.Controls.Add(c); // will add the control c to the other form
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 am using a SplitContainter in MDI parent Form.
My problem is I loaded a form in panel1 named First Form. In this First Form with a button I load SecondForm in panel2.
I am using this code:
Form In_but = new SecondForm();
In_but.MdiParent = this.ParentForm;
In_but.TopLevel = false;
this.splitContainer1.Panel2.Controls.Add(In_but);
In_but.Show();
But it's not working. The error is: does not contain definition splitContainer1.
From looking at your code sample, I suspect your problem is when you refer to this.splitContainer, this is your 'First form' on panel 1, and your SplitContainer is on this.ParentForm.
I'd suggest changing that line to this.(ParentForm as <whatever class your parent form is>).splitContainer1.Panel2.Controls.Add(In_but);
try this
frmChild frmChild = new frmChild();
frmChild.TopLevel = false;
frmChild.Parent = this.splitContainer3.Panel2;
frmMasterlistAdministrationAdd.Show();
frmTest fs = new frmTest(); //frmTest is the form that you going to call
fs.MdiParent = this; //the main form is a mdiform and have a splitcontainer with
//two panels
this.splitContainer1.Panel2.Controls.Add(fs); //add the fs form to the panel2
fs.Show(); //show the form