Runtime Button_Click event

Ok. I have created a button at runtime, the only problem is, I need it to have a click event. How do I go about doing that


Answer this question

Runtime Button_Click event

  • mediaman

    What are you trying to do, exactly Maybe there's a simpler way that doesn't require to create controls at runtime in the first place...

  • thunderbird

    try this

    public Form1()

    {

    InitializeComponent();

    Button button = new Button();

    button.Text = "Close";

    button.Click += new EventHandler(button_Click);

    Controls.Add(button);

    }

    void button_Click(object sender, EventArgs e)

    {

    this.Close();

    }

    Andres.


  • emz chisnky

    Well, you can you please describe an easier way.

  • _aaa_

    Well. Im still having problems. I have a peice of code here, its kind of long, but could you take a look at it and tell me what the problem is

    private void timer1_Tick(object sender, EventArgs e)

    {

    if (progressBar1.Value <= progressBar1.Maximum - 1)

    progressBar1.Value += 1;

     

    else

    {

    timer1.Enabled = false;

    progressBar1.Visible = false;

    label1.Visible = false;

     

    Label label2 = new Label();

    label2.Text = "text";

    label2.Location = new Point(125, 9);

    label2.Size = new Size(229, 19);

    label2.Font = new Font("Microsoft San Serif", 10, FontStyle.Bold);

    label2.Image = CSharp.Properties.Resources.ColorBackground;

    label2.ForeColor = Color.AntiqueWhite;

    this.Controls.Add(label2);


    Label
    label3 = new Label();

    label3.Text = "Welcome Message";

    label3.Location = new Point(345, 431);

    label3.Size = new Size(249, 23);

    label3.Font = new Font("Microsoft San Serif", 10, FontStyle.Bold);

    label3.Image = CSharp.Properties.Resources.ColorBackground;

    label3.ForeColor = Color.AntiqueWhite;

    label3.BringToFront();

    this.Controls.Add(label3);


    Label
    label4 = new Label();

    label4.Text = "text

    label4.Location = new Point(169, 80);

    label4.Size = new Size(442, 50);

    label4.Font = new Font("Microsoft San Serif", 11, FontStyle.Regular);

    label4.Image = CSharp.Properties.Resources.CSharp_copy;

    label4.ForeColor = Color.White;

    this.Controls.Add(label4);


    Label
    label5 = new Label();

    label5.Text = "text.";

    label5.Location = new Point(171, 136);

    label5.Size = new Size(442, 74);

    label5.Font = new Font("Microsoft San Serif", 11, FontStyle.Regular);

    label5.Image = CSharp.Properties.Resources.CSharp_copy;

    label5.ForeColor = Color.White;

    this.Controls.Add(label5);


    Button
    button1 = new Button();

    button1.Text = "text";

    button1.Location = new Point(176, 204);

    button1.Size = new Size(104, 23);

    button1.Font = new Font("Microsoft San Serif", 8, FontStyle.Regular);

    button1.FlatStyle = FlatStyle.Flat;

    button1.TextAlign = ContentAlignment.MiddleCenter;

    button1.ForeColor = Color.SteelBlue;

    button1.Click += new EventHandler(button1_Click);

    this.Controls.Add(button1);

    }

    }

    private void button1_Click(object sender, EventArgs e)

    {

    label3.Visible = false;

    label4.Visible = false;

    label5.Visible = false;

    }



  • AdLearning.Net

    I tried your code above by creating a new Windows Application, adding a progress bar and a few labels to it, and creating a new timer.  The code does what I expected it to; the progress bar inches forward based on the tick event.  When the progress bar is full, it disappears along with a few labels and a button appears.  Clicking on the button removes the remaining labels.

    What is the problem that you're having with it

    Thanks,
    Anson Horton [ansonh@microsoft.com]
    Visual C# IDE PM

  • restore

    private void button1_Click(object sender, EventArgs e)

    {

    for(int i = 0;i<this.Controls.Length;i++)
    {
       Label lbl = this.Controls[ i ] as Label;
       if(lbl != null)
          If(lbl.Text = "Put here what do you like")
                lbl.Visible = false;
    }

    }


    This is only one solution, there is more options to do this.



  • Runtime Button_Click event