Add a Button Control Pragrmatically in C#

Today in C#, we will create a program that will add a button pragmatically in windows form using C#. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. You don't have to design a button in your form because we will just code to have a button when loading the form. 3. Put this code in your code view.
  1. //set a class variable to be used during the form
  2. private int Count_control = 0;
  3. private Point Location_control = new Point(10, 50);
After that, create a method for creating a Button.
  1. private void create_button()
  2. {
  3.  
  4. //increment the count_control.
  5. Count_control += 1;
  6. //checking if the buttons has reached to 5 or not
  7. if (Count_control <= 5)
  8. {
  9. //set a new button
  10. Button new_Button = new Button();
  11. //add the properties of the button
  12. new_Button.Name = "Button" + Count_control.ToString();
  13. new_Button.Text = "Button" + Count_control.ToString();
  14. new_Button.Location = new Point(Location_control.X + 10, Location_control.Y);
  15. new_Button.Width = 180;
  16. Location_control.Y += new_Button.Height + 10;
  17. //create the event handler
  18. new_Button.Click += new System.EventHandler(myButtonHandler_Click);
  19. //add the new button to the collection of controls
  20. Controls.Add(new_Button);
  21. }
  22. else
  23. {
  24.  
  25. //checking if you want to clear the controls that you have added.
  26. if (MessageBox.Show("You\'ve reached 5 controls. Do you want to clear controls to start again?", "Proceed", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == Windows.Forms.DialogResult.OK)
  27. {
  28.  
  29. Controls.Clear(); //clearing the control
  30. Count_control = 0; //returning the count_control to its default value
  31. Location_control = new Point(10, 50); //set a new point of the controls
  32. create_button(); //put a control so that you can add another controls
  33. }
  34. }
  35. }
Then, create a subroutine that handles the click events of the Buttons.
  1. private void myButtonHandler_Click(object sender, EventArgs e)
  2. {
  3. //verifying the buttons
  4. if (sender is Button)
  5. {
  6. create_button(); //create a new button
  7.  
  8. }
  9. }
Lastly, you have to put your method that you have created in the first load so that the button will appear in the Form.
  1. private void Form1_Load(System.Object sender, System.EventArgs e)
  2. {
  3. create_button();
  4. }
Output: outputoutput Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment