how to create a panel at runtime which includes button textbox label timer using c#.net

844 views Asked by At
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.

2

There are 2 answers

0
TaW On BEST ANSWER

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:

if (c.GetType() == typeof(Button))
{
    c2 = new Button();
    c2.Click += cloneButtonsClick;
}

This creates a common Click event for all Buttons.

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 its Name (if you have set it) or its Text. (Or any other property you have set, like e.g. the Tag) After casting sender to Button you can do the tests and code your click actions..:

void cloneButtonsClick(object sender, EventArgs e)
{
     Button bt = sender as Button;
     if (bt == null) return;                           // this should never happen!
     /* if (bt.Name == "saveButton")  { do your things; } // one way to test..   */
     if (bt.Text== "Save")         { do your things; } // ..another way to test
     else if (bt.Text== "Load")    { do your things; } // ..another way to test
     //..
    }
0
al_amanat On

You have many options to create a panel in application. If I get you right, you need to get buttons in another block of code. So you can use linq-expression:

var buttons = this.Controls.OfType<Panel>().Where(x => x is Panel).SelectMany(x => x.Controls).OfType<Button>();

Or you can store them in local variable to quick access, when you create it:

if (c.GetType() == typeof(Button))
{
    c2 = new Button();
    buttons.Add(c2);     // where buttons is List<Button>();
}

But I think the best descision will be to create a UserControl (copy of panel1) instead of multiply instances of dynamic panel and extract some properies/events to outside.