Link to home
Start Free TrialLog in
Avatar of Parth48
Parth48Flag for India

asked on

how can i create button click event dynamically through C# coding in window application ??

here i create 3 buttons dynamically in form load event ...

please refer the below code ...

private void Form1_Load(object sender, EventArgs e)
        {
            //get form properties
            objGeneral.GetFormProperties(this.FindForm());
            //Dynamically create buttons
            
            //button 1
            Button btnSqlServerCheck = new Button();
            btnSqlServerCheck.Text = "Check MS SQL Server Installation";
            btnSqlServerCheck.Location = new Point(45, 27);
            btnSqlServerCheck.Size = new Size(200, 25);
            this.Controls.Add(btnSqlServerCheck);

            //button 2
            Button btnAccessCheck = new Button();
            btnAccessCheck.Text = "Check MS Access Installation";
            btnAccessCheck.Location = new Point(45, 70);
            btnAccessCheck.Size = new Size(200, 25);
            this.Controls.Add(btnAccessCheck);

            //button 3
            Button btnExcelCheck = new Button();
            btnExcelCheck.Text = "Check MS Excel Installation";
            btnExcelCheck.Location = new Point(45, 113);
            btnExcelCheck.Size = new Size(200, 25);
            this.Controls.Add(btnExcelCheck);

            //button click events
        }

Open in new window


but now how can i create button click event for above buttons dynamically through coding ??

give me some suggestions ...
Avatar of Kalpesh Chhatrala
Kalpesh Chhatrala
Flag of India image

you can create event in code behind.

sample
         button1.Click += new EventHandler(button1_Click);

        void button1_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

Open in new window

Sample

private void Form1_Load(object sender, EventArgs e)
        {
            //get form properties
            objGeneral.GetFormProperties(this.FindForm());
            //Dynamically create buttons
            
            //button 1
            Button btnSqlServerCheck = new Button();
            btnSqlServerCheck.Text = "Check MS SQL Server Installation";
            btnSqlServerCheck.Location = new Point(45, 27);
            btnSqlServerCheck.Size = new Size(200, 25);
            this.Controls.Add(btnSqlServerCheck);

            //button 2
            Button btnAccessCheck = new Button();
            btnAccessCheck.Text = "Check MS Access Installation";
            btnAccessCheck.Location = new Point(45, 70);
            btnAccessCheck.Size = new Size(200, 25);
            this.Controls.Add(btnAccessCheck);

            //button 3
            Button btnExcelCheck = new Button();
            btnExcelCheck.Text = "Check MS Excel Installation";
            btnExcelCheck.Location = new Point(45, 113);
            btnExcelCheck.Size = new Size(200, 25);
            this.Controls.Add(btnExcelCheck);

            //button click events
            btnExcelCheck.Click += new EventHandler(btnExcelCheck_Click);
        }
        void btnExcelCheck_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of crysallus
crysallus
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

private void Form1_Load(object sender, EventArgs e)
        {
            //get form properties
            objGeneral.GetFormProperties(this.FindForm());
            //Dynamically create buttons

            //button 1
            Button btnSqlServerCheck = new Button();
            btnSqlServerCheck.Text = "Check MS SQL Server Installation";
            btnSqlServerCheck.Location = new Point(45, 27);
            btnSqlServerCheck.Size = new Size(200, 25);
            this.Controls.Add(btnSqlServerCheck);

            //button click events
            btnSqlServerCheck.Click += new EventHandler(btnSqlServerCheck_Click);

            //button 2
            Button btnAccessCheck = new Button();
            btnAccessCheck.Text = "Check MS Access Installation";
            btnAccessCheck.Location = new Point(45, 70);
            btnAccessCheck.Size = new Size(200, 25);
            this.Controls.Add(btnAccessCheck);

            //button click events
            btnAccessCheck.Click += new EventHandler(btnAccessCheck_Click);

            //button 3
            Button btnExcelCheck = new Button();
            btnExcelCheck.Text = "Check MS Excel Installation";
            btnExcelCheck.Location = new Point(45, 113);
            btnExcelCheck.Size = new Size(200, 25);
            this.Controls.Add(btnExcelCheck);

            //button click events
            btnExcelCheck.Click += new EventHandler(btnExcelCheck_Click);
        }
        void btnExcelCheck_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void btnAccessCheck_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void btnSqlServerCheck_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

Open in new window