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
Related
I'm working on a Agenda using windows forms C#, I'm trying to create a colored picture box for each appointment object in the project. Using this code that is used in a loop for each object in my appointment list im creating each picturebox on the right location on the form1 screen.
PictureBox Point = new PictureBox();
this.Controls.Add(Point);
Point.Location = new Point(obj.Location.X, 45 + obj.Location.Y);
Point.BackColor = color;
Point.Size = new Size(96, 25);
Point.Enabled = false;
Point.Tag = "Point";
Point.TabIndex = 100;
Point.Visible = true;
When I'm calling this method from input on the same form, for example a button click. It will work just fine and create all the picture boxxes as needed. But when I'm calling it from the form2.closed event it wont work. Form 2 is my appointment planner form, when clicking on save on this form it will add a new object to the list, so a new picturebox should be created. I have checked the debug using breakpoints, and strange enough it will go through the create code, but no matter what I do it wont render the pictureboxxes.
I personally think it has to do with the form1 not Initializing when called from form2.closed event. But even when using InitializeComponent(); end Refresh(); inside my code it still doesnt work.
Am I using the wrong event or is there a specific call I need to make to generate the pictureboxxes?
Sorry if my post is lacking code or info, I'm not used to posting on stackoverflow, feel free to ask for more information if needed.
if you run your code from Form2 and you want to add control to Form1 you cannot use "this". Form1 has to be accessible from Form2.
PictureBox Point = new PictureBox();
Point.Location = new Point(obj.Location.X, 45 + obj.Location.Y);
Point.BackColor = color;
Point.Size = new Size(96, 25);
Point.Enabled = false;
Point.Tag = "Point";
Point.TabIndex = 100;
Point.Visible = true;
Form1.Controls.Add(Point);
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);
My problem is not complicated but i've been looking up for quite some time now and can't seem to find the correct way to phrase it so if this is a duplicate I am sorry.
I have a Form created on a button click, within this Form is a table layout which contains a set of Controls (a label, a combo box and a button). What I want to do is to store the current index selected in the combo box when the button is pressed. However, I can't access the combo box when I'm in the button click event handling block.
This is the creation of my Form, nothing extraordinary.
Form settingsWindow = new Form();
settingsWindow.Text = "Connexion settings";
settingsWindow.MaximumSize = new System.Drawing.Size(200, 200);
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
Label label = new Label();
label.Text = "Choose an available COM port";
Button bt_OK = new Button();
bt_OK.Text = "valider";
bt_OK.Anchor = AnchorStyles.Bottom;
bt_OK.Name = "bt_OK";
bt_OK.Click += Bt_OK_Click;
ComboBox cb_portList = new ComboBox();
SerialComm serial = new SerialComm(); //Filling the combo box with a list of
List<string> portList = new List<string>(); //available COM ports
portList = serial.getAllPorts();
foreach(string portName in portList)
{
cb_portList.Items.Add(portName);
}
layout.Controls.Add(label);
layout.Controls.Add(cb_portList);
layout.Controls.Add(bt_OK);
What I'd like to be able to do is the following, imagine I made a class to store the data I want :
private void Bt_OK_Click(object sender, EventArgs e)
{
datacontainer.selectItem = cb_portList.SelectedItem;
settingsWindow.close();
}
Am I not able to do so because my Form is already created within an eventhandler ? Or is it just because they're in separate blocks. If so is there a way the new Form and its controls available from outside the block ?
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 want to create and save a windows form with some controls from another windows form.
I tried below code but it's only create a new form with a textbox at runtime but i am not able to save this form in same solution path.
using (Form form = new Form())
{
form.Text = "About Us";
TextBox tb = new TextBox();
form.Controls.Add(tb);
form.ShowDialog();
}