Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dynamically Adding Button to a Form

0.00/5 (No votes)
15 Dec 2006 1  
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.

// 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.

// 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.

//
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.

// 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here