?
Solved

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

Posted on 2011-03-01
4
Medium Priority
?
1,293 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 2000 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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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