Click here to Skip to main content
15,922,584 members
Articles / Programming Languages / C#

Dynamically Adding Button to a Form

Rate me:
Please Sign up or sign in to vote.
2.61/5 (17 votes)
15 Dec 2006CPOL 42.6K   12  
How to dynamically add a button to a Windows Form.

Introduction

I was writing a small C# application that required buttons to appear depending on a configuration file. I did a quick search of the internet and could not find much, so I thought I would put something simple together.

Background

The concept of adding a button is exactly the same as adding any control to a form (WinForms or WPF form).

Using the Code

Below is a simple piece of code that creates an instance of a button, changes some properties, and adds it to the form.

C#
// Create Instance of a Button
Button myButton = new Button();

// Set some properties

myButton.Visible = true; 

// Button Size
myButton.Width = 100; 
myButton.Height = 20 

// Positon
myButton.Left = 100; 
myButton.Top = 100; 

myButton.Location = new Point(Left, Top); 

// Give to button a name
myButton.Name = Name; 

// Text to appear on Button
myButton.Text = "My Button"; 

// Make the text look nice
Font myFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold); 
myButton.Font = myFont;

// This line adds this instance of a button to the form
this.Controls.Add(myButton);

For WCF, we need to do something slightly different, as the object model is different.

C#
// Create my Button
Button myButton = new Button();

// Create a grid to go on the button
Grid subGrid = new Grid();

// Create a text block and image to go on the grid
TextBlock myTextBlock = new TextBlock();
Image myImage = new Image();

// Get an image from a file
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("c:\\mylogo.jpg");
bi.EndInit();
// myImage3.Stretch = Stretch.Fill;
myImage.Source = bi;

//put some text on the button
myTextBlock.Text = "My Button";

// add the image and text bock to the grid
subGrid.Children.Add(myImage);
subGrid.Children.Add(myTextBlock);

// set the grid to the content of the button
myButton.Content = subGrid;

// add the button to a grid on the form called grid1.
grid1.Children.Add(myButton);

That's okay up to a point, but the button doesn't do anything. We now need to associate the click event with some code.

C#
//
myButton.Click += new System.EventHandler(this.Button_Click_Code);

// Create a new function
private void Button_Click_Code(object sender, EventArgs e) 
{
     MessageBox.Show("You clicked my button");
}

If you create multiple buttons and associate them with the same Button_Click_Code function, there needs to be some way to work out which button you pressed.

C#
// Create a new function
private void Button_Click_Code(object sender, EventArgs e) 
{
    // Try something like this   
    Button myBut = (Button)sender;
    MessageBox.Show(myBut.Name);

    switch (myBut.Name)
    {
        case "button_one" : 
            // do something
            break;
        case "button_two" :
            // do something else
             break;
        default:
            // everything else
            break;
    }
}

Points of Interest

The idea of dynamically adding controls to a form is the same as above, any object of any type can be added to a form.

Updates

  • 15/Dec/2006: Added some code for a WPF button.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
I've been a programmer for longer than I care to remember. It all started with those BBC (model B's) at school, it all went down hill from there.....

This year, I am mostly coding in.. C# (WPF & WCF)

Blog Address http://markpm.blogspot.com


Comments and Discussions

 
-- There are no messages in this forum --