How to handle events of Buttons created at run time

Hi,

I am creating buttons at the form load event of a form, Caption and ID of these buttons come from a database, I want to handle events of different buttons where each button perfirms different operation.

Thanks and regards,

kapil...



Answer this question

How to handle events of Buttons created at run time

  • Ronen Y

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                Button btn = new Button();
                btn.Text = "Click me";
                btn.Click += new EventHandler(btn_Click);
                this.Controls.Add(btn);
            }

            void btn_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Clicked");
            }
        }
    }


  • bryanandrews

    When creating the buttons, store some form of ID in the buttons' Tag property. Set the Click event of each to the same method, which would look like this:

    void btn_Click(object sender, EventArgs e)
    {
    Button btn = sender as Button;
    MyObj obj = LookupInDatabase(btn.Tag);
    this.Textbox1.Text = obj.Caption;
    }



  • smaillet

    LucVK's answer is correct for WinForms, but if you are working with a WebForm, that step must be done in the CreateChildControls() virtual function.

    (As a side note, I'd always a good idea to mention if you are using a WinForm or a WebForm)




  • groot

    H Ali Raza Shaikhi,

    Thanks for the reply, acctually my concern is, how to handle different click events for different buttons which comes from the database, take an example:-

    Suppose I have 10 names in the database, for these names 10 buttons are created at run time, now I want to display the caption of each button in a text box or label cotrol in the click event of each button.


  • Binoy R B

    James Curran wrote:

    When creating the buttons, store some form of ID in the buttons' Tag property. Set the Click event of each to the same method, which would look like this:

    void btn_Click(object sender, EventArgs e)
    {
    Button btn = sender as Button;
    MyObj obj = LookupInDatabase(btn.Tag);
    this.Textbox1.Text = obj.Caption;
    }

    In case he just wants the caption of the button he could as well use this :

    void btn_Click(object sender, EventArgs e)
    {
    this.Textbox1.Text = ((Button)sender).Text;
    }

    Or in case the textbox differs depending on the button that was clicked :

    ((TextBox)this.Controls.Find(s, true)[0]).Text = "";

    With s being the string containing the name of the textbox - e.g. string.Concat("tb", ((Button)sender).Name)



  • David Wilson

  • How to handle events of Buttons created at run time