Solved

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

Posted on 2011-03-01
4
1,281 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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