private void Add_Timer_Click(object sender, EventArgs e)
{
number_of_timer++;
for (int i = 1; i < number_of_timer; i++)
{
Panel pnl = new Panel();
Control c2 = new Control();
pnl.Location = new Point(12, 175*i+25);
pnl.BorderStyle = panel1.BorderStyle;
pnl.BackColor = panel1.BackColor;
pnl.Size = panel1.Size;
pnl.Visible = true;
foreach (Control c in panel1.Controls)
{
if (c.GetType() == typeof(TextBox))
c2 = new TextBox();
if (c.GetType() == typeof(Button))
c2 = new Button();
if (c.GetType() == typeof(Label))
c2 = new Label();
if(c.GetType()== typeof(Timer))
Timer.Tick += new EventHandler(Timer_Tick);
c2.Location = c.Location;
c2.Size = c.Size;
c2.Font = c.Font;
c2.Text = c.Text;
c2.Name = c.Name;
pnl.Controls.Add(c2);
this.Controls.Add(pnl);
}
}
}
I have created a panel using this but I am unable to access the buttons which are created at runtime.
By unable to access the Buttons I guess you mean you don't have a
Click
event for them?You simply need to add it, like you did for the
Timer
:This creates a common
Click
event for allButtons
.Therefore in the event you will need to check on which button it was that was
pressed
, assuming there are more than one. You can do that by itsName
(if you have set it) or itsText
. (Or any other property you have set, like e.g. theTag
) After castingsender
toButton
you can do the tests and code your click actions..: