Solved

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

Posted on 2011-03-01
4
1,256 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
work allocation; web development; vba; access; 4 46
Create XML 5 35
Re-position the objects 7 55
Red error squiggly on vb.net 7 14
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

10 Experts available now in Live!

Get 1:1 Help Now