Add label to Panel programmatically - c#

So I have a form, and I want to add some Panels with some controls(labels, and radiobuttons) when the form loads.
And I want to do it from the code, of course(it's for making an application with tests, and the questions will be random)
This is what I have done till now:
List<Panel>ls=new List<Panel>();
private void VizualizareTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
ls.Add(pan);
Label l = new Label();
l.Text = "l"+i;
pan.Controls.Add(l);
pan.Show();
}
}
But it doesn't show anything on the form.

Add the panel just created to the Form.Controls collection
private void VizualizareTest_Load(object sender, EventArgs e)
{
for (int i = 0; i < 4; i++)
{
Panel pan = new Panel();
pan.Name = "panel" + i;
ls.Add(pan);
Label l = new Label();
l.Text = "l"+i;
pan.Location = new Point(10, i * 100);
pan.Size = new Size(200, 90); // just an example
pan.Controls.Add(l);
this.Controls.Add(pan);
}
}

enter image description here
private void button2_Click(object sender, EventArgs e)
{
int X = 153;
int Y = 34;
for (int i = 1; i < 4; i++)
{
Panel pnl = new Panel();
pnl.SuspendLayout();
pnl.Location = new Point(X, Y);
pnl.Name = "pnl"+i;
pnl.Size = new Size(200, 57);
pnl.BorderStyle = BorderStyle.FixedSingle;
Label lbl = new Label();
lbl.Location = new Point(X - 100, Y - 17);
lbl.Name = "lbl" + i;
lbl.Size = new Size(75, 23);
lbl.Text = "lable_" +i;
pnl.Controls.Add(lbl);
pnl.ResumeLayout(false);
this.Controls.Add(pnl);
Y = Y + 95;
}
}
why not display label2 & label3?

Related

Dynamically update button

I am attempting to dynamically add a list of buttons:
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
int count = 1;
int x = 0;
int y = 0;
for (int i = 0; i < 5; i++)
{
Button btn = new Button();
btn.Text = count.ToString();
btn.Name = count.ToString();
btn.Size = new Size(35, 35);
btn.Location = new Point(150, 150 * y);
//btn.Dock = DockStyle.Fill;
y++;
count++;
Controls.Add(btn);
}
}
}
Unfortunately this does not apply any buttons to the form.
In addition I was wondering how could I append these buttons in a panel called subPanelClient
This worked for me, the issue was the position of the button, as you have to indicate it within a panel or form.
In this case i just docked them to the panel
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
//Debug.WriteLine(clientNames[0]);
int basex = subPanelClient.Location.X;
int basey = subPanelClient.Location.Y;
subPanelClient.Controls.Clear();
Debug.WriteLine(clientNames.Count);
for (int i = 0; i < clientNames.Count; i++)
{
Button b = new Button();
b.Left = basex;
b.Top = basey;
b.Size = new Size(25, 25); // <== add this line
b.Dock = DockStyle.Top;
b.ForeColor = Color.Gainsboro;
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
b.Padding = new Padding(35, 0, 0, 0);
b.TextAlign = ContentAlignment.MiddleLeft;
basey += 25;
b.Name = clientNames[i];
b.Text = clientNames[i];
subPanelClient.Controls.Add(b);
buttonsAdded.Insert(i, b);
}
}
}

Increment/Decrement value from NumericUpDown

I have the following problem. I create a form with two NumericUpDown and I want to increment/decrement the value from them by 0.01. I tried to use NumericUpDownObject.Increment=0.01M, but there is no change after that, the value don't change. This is the code:
namespace Proiect1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 1;
int o = 1;
private void button1_Click(object sender, EventArgs e)
{
Label l = new Label();
Label l2 = new Label();
NumericUpDown t = new NumericUpDown();
NumericUpDown t2 = new NumericUpDown();
l.Top = i * 25;
l.Left = 3;
l2.Top = i * 25;
l2.Left = 63;
t.Value = 0;
t.Increment = 0.01M;
t.Top = i * 25;
t.Left = 30;
t2.Value = 0;
t2.Top = i * 25;
t2.Left = 90;
i++;
t.Size =new System.Drawing.Size(30, 30);
t2.Size = new System.Drawing.Size(30, 30);
l.Size = new System.Drawing.Size(25, 20);
l2.Size = new System.Drawing.Size(29, 20);
l.Text = "n" + o;
l2.Text = "w" + o;
o++;
panel1.Controls.Add(t);
panel1.Controls.Add(t2);
panel1.Controls.Add(l);
panel1.Controls.Add(l2);
panel1.AutoScroll = true;
}
Try this
This will setup the whole form when it's created, but only increment when clicked.
It looks like your solution is re-creating everything each time it's clicked.
P.s apols - been a while since I did Winforms/WPF
namespace Proiect1
{
public partial class Form1 : Form
{
NumericUpDown _t = new NumericUpDown();
NumericUpDown _t2 = new NumericUpDown();
Label _l = new Label();
Label _l2 = new Label();
public Form1()
{
InitializeComponent();
int i = 1;
int o = 1;
_l.Top = i * 25;
_l.Left = 3;
_l2.Top = i * 25;
_l2.Left = 63;
_t.Value = 0;
_t.Increment = 0.01;
_t.Top = i * 25;
_t.Left = 30;
_t2.Value = 0;
_t2.Top = i * 25;
_t2.Left = 90;
i++;
_t.Size =new System.Drawing.Size(30, 30);
_t2.Size = new System.Drawing.Size(30, 30);
_l.Size = new System.Drawing.Size(25, 20);
_l2.Size = new System.Drawing.Size(29, 20);
_l.Text = "n" + o;
_l2.Text = "w" + o;
o++;
panel1.Controls.Add(_t);
panel1.Controls.Add(_t2);
panel1.Controls.Add(l_);
panel1.Controls.Add(l_2);
panel1.AutoScroll = true;
}
private void button1_Click(object sender, EventArgs e)
{
_t.Increment = 0.01;
}

Why will this code not work when trying to add labels to panels

The screenshot shows part of my code and I am getting the error cannot be used as a method and I'm not sure how to fix it. I want to add labels to the panels that are created for a calendar. Any help will be appreciated.
public partial class frmDisplayAppointment : Form
{
public frmDisplayAppointment()
{
InitializeComponent();
}
private List<FlowLayoutPanel> listFlDay = new List<FlowLayoutPanel>();
private void DisplayAppointment_Load(object sender, EventArgs e)
{
GenerateDayPanel(42);
AddLabelDayToFlDay(1);
}
private void GenerateDayPanel(int totalDays)
{
flDays.Controls.Clear();
for (int i = 1; i <= totalDays; i++)
{
FlowLayoutPanel fl = new FlowLayoutPanel();
fl.Name = $"flDay{i}";
fl.Size = new Size(94, 70);
fl.BackColor = Color.White;
fl.BorderStyle = BorderStyle.FixedSingle;
flDays.Controls.Add(fl);
listFlDay.Add(fl);
}
}
private void AddLabelDayToFlDay(int startDay)
{
int day = 1;
for (int i = startDay; i <= listFlDay.Count - 1; i++)
{
Label lbl = new Label();
lbl.Name = $"lblDay{i}";
lbl.AutoSize = false;
lbl.TextAlign = ContentAlignment.MiddleRight;
lbl.Size = new Size(95, 23);
lbl.Text = day.ToString() ;
//ERROR HERE
listFlDay(i).Controls.Add(lbl);
day += 1;
}
}
Many thanks

couldn't change active tabPage with button c#

I have a MetroTabControl named mtcNewExpTabControl with four pages(mtpSettings, mtpNewExp, mtpTempImages, mtpCompareImages). I am starting New Experiment on mtpNewExp page and get image after clicking "StartExperiment" button. User can get many images. After completion of experiment, I add images to imageList1.Images.
When I click on the mtpTempImages tabPage mtcNewExpTabControl_SelectedIndexChanged triggered and images are displayed with combobox named with imageList1.Images.Keys[i]. I can show many combobox and picturebox pairs on mtpTempImages.
On mtpTempImages there is also a mtbCompareImages button. User can check checkboxes next to the picturebox named same with checboxName. After checking all the checkboxes needed to compared, user will click on the "Compare Images" button. I want to change active tabPage to mtpCompareImages without clicking on the tab. mtbCompareImages button should be enough for that. I can show images on mtpTempImages but I could not succeded that on mtpCompareImages tabpage. Active tabPage also do not change to mtpCompareImages tabPage. What should I do?
List<MetroCheckBox> cbxTempImages = new List<MetroCheckBox>();
List<MetroCheckBox> cbxCompareImages = new List<MetroCheckBox>();
List<PictureBox> pbxTempImages = new List<PictureBox>();
List<PictureBox> pbxCompareImages = new List<PictureBox>();
private void mtbCompareImages_Click(object sender, EventArgs e)
{
for (int i = 0; i < cbxTempImages.Count(); i++)
{
if (cbxTempImages[i].Checked == true)
{
imageListChecked.Images.Add(pbxTempImages[i].Name, pbxTempImages[i].Image);
}
}
this.mtcNewExpTabControl.SelectedTab = mtpCompareImages;
}
private void mtcNewExpTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if((MetroTabPage)this.mtcNewExpTabControl.SelectedTab == mtpTempImages)
{
for (int i = 0; i < imageList1.Images.Count; i++)
{
cbxTempImages.Add(new MetroCheckBox());
cbxTempImages[i].Name = imageList1.Images.Keys[i].ToString();
cbxTempImages[i].Size = new Size(15, 15);
cbxTempImages[i].BackColor = Color.Transparent;
cbxTempImages[i].Location = new Point(x, y);
//PictureBox pic = new PictureBox();
pbxTempImages.Add(new PictureBox());
pbxTempImages[i].Name = imageList1.Images.Keys[i].ToString();
pbxTempImages[i].Image = imageList1.Images[i];
pbxTempImages[i].SizeMode = PictureBoxSizeMode.StretchImage;
pbxTempImages[i].Location = new Point(x + 15, y);
x += 120;
if (x > this.pnlTempImages.Width - 120)
{
x = 10; y += 100;
}
this.pnlTempImages.Controls.Add(cbxTempImages[i]);
this.pnlTempImages.Controls.Add(pbxTempImages[i]);
}
}
else if((MetroTabPage)this.mtcNewExpTabControl.SelectedTab == mtpCompareImages)
{
x = 10; y = 10;
for (int i = 0; i < imageListChecked.Images.Count; i++)
{
cbxCompareImages.Add(new MetroCheckBox());
//MetroCheckBox cbxCI = new MetroCheckBox();
cbxCompareImages[i].Name = imageListChecked.Images.Keys[i].ToString();
cbxCompareImages[i].Size = new Size(15, 15);
cbxCompareImages[i].BackColor = Color.Transparent;
cbxCompareImages[i].Location = new Point(x, y);
//PictureBox picCI = new PictureBox();
pbxCompareImages.Add(new PictureBox());
pbxCompareImages[i].Name = imageListChecked.Images.Keys[i].ToString();
pbxCompareImages[i].Image = imageListChecked.Images[i];
pbxCompareImages[i].SizeMode = PictureBoxSizeMode.StretchImage;
pbxCompareImages[i].Location = new Point(x + 15, y);
x += 120;
if (x > this.pnlCompareImages.Width - 120)
{
x = 10; y += 100;
}
this.pnlCompareImages.Controls.Add(cbxCompareImages[i]);
this.pnlCompareImages.Controls.Add(pbxCompareImages[i]);
}
}
InitializeComponent();
}

How to add buttons dynamically to my form?

I want to create 10 buttons on my form when I click on button1. No error with this code below but it doesnt work either.
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < buttons.Capacity; i++)
{
this.Controls.Add(buttons[i]);
}
}
You aren't creating any buttons, you just have an empty list.
You can forget the list and just create the buttons in the loop.
private void button1_Click(object sender, EventArgs e)
{
int top = 50;
int left = 100;
for (int i = 0; i < 10; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
this.Controls.Add(button);
top += button.Height + 2;
}
}
It doesn't work because the list is empty. Try this:
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < 10; i++)
{
Button newButton = new Button();
buttons.Add(newButton);
this.Controls.Add(newButton);
}
}
You could do something like this:
Point newLoc = new Point(5,5); // Set whatever you want for initial location
for(int i=0; i < 10; ++i)
{
Button b = new Button();
b.Size = new Size(10, 50);
b.Location = newLoc;
newLoc.Offset(0, b.Height + 5);
Controls.Add(b);
}
If you want them to layout in any sort of reasonable fashion it would be better to add them to one of the layout panels (i.e. FlowLayoutPanel) or to align them yourself.
Two problems- List is empty. You need to add some buttons to the list first. Second problem: You can't add buttons to "this". "This" is not referencing what you think, I think. Change this to reference a Panel for instance.
//Assume you have on your .aspx page:
<asp:Panel ID="Panel_Controls" runat="server"></asp:Panel>
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < buttons.Capacity; i++)
{
Panel_Controls.Controls.Add(buttons[i]);
}
}
use button array like this.it will create 3 dynamic buttons bcoz h variable has value of 3
private void button1_Click(object sender, EventArgs e)
{
int h =3;
Button[] buttonArray = new Button[8];
for (int i = 0; i <= h-1; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Size = new Size(20, 43);
buttonArray[i].Name= ""+i+"";
buttonArray[i].Click += button_Click;//function
buttonArray[i].Location = new Point(40, 20 + (i * 20));
panel1.Controls.Add(buttonArray[i]);
} }
I had the same doubt and came up with the following contribution:
int height = this.Size.Height;
int width = this.Size.Width;
int widthOffset = 10;
int heightOffset = 10;
int btnWidth = 100; // Button Widht
int btnHeight = 40; // Button Height
for (int i = 0; i < 50; ++i)
{
if ((widthOffset + btnWidth) >= width)
{
widthOffset = 10;
heightOffset = heightOffset + btnHeight
var button = new Button();
button.Size = new Size(btnWidth, btnHeight);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click; // Button Click Event
button.Location = new Point(widthOffset, heightOffset);
Controls.Add(button);
widthOffset = widthOffset + (btnWidth);
}
else
{
var button = new Button();
button.Size = new Size(btnWidth, btnHeight);
button.Name = "" + i + "";
button.Text = "" + i + "";
//button.Click += button_Click; // Button Click Event
button.Location = new Point(widthOffset, heightOffset);
Controls.Add(button);
widthOffset = widthOffset + (btnWidth);
}
}
Expected Behaviour:
This will generate the buttons dinamically and using the current window size, "break a line" when the button exceeds the right margin of your window.
First, you aren't actually creating 10 buttons. Second, you need to set the location of each button, or they will appear on top of each other. This will do the trick:
for (int i = 0; i < 10; ++i)
{
var button = new Button();
button.Location = new Point(button.Width * i + 4, 0);
Controls.Add(button);
}
You can't add a Button to an empty list without creating a new instance of that Button.
You are missing the
Button newButton = new Button();
in your code plus get rid of the .Capacity

Categories