Solved

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

Posted on 2011-03-01
4
1,263 Views
Last Modified: 2012-05-11
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 ...
0
Comment
Question by:Parth48
  • 2
4 Comments
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 35014709
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

0
 
LVL 16

Expert Comment

by:Kalpesh Chhatrala
ID: 35014715
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

0
 
LVL 8

Accepted Solution

by:
crysallus earned 500 total points
ID: 35014728
Just add a method name to the Click event for each button eg.

buttonName.Click += new System.EventHandler(yourMethodName);

Open in new window


then add methods to Form1 to handle the click event's for each button with the method signature

private void yourMethodName(object sender, EventArgs e)
{
    ...
}

Open in new window

0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35014760

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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now