//set a class variable to be used during the form private int Count_control = 0;
private void create_button() { //increment the count_control. Count_control += 1; //checking if the buttons has reached to 5 or not if (Count_control <= 5) { //set a new button //add the properties of the button new_Button.Name = "Button" + Count_control.ToString(); new_Button.Text = "Button" + Count_control.ToString(); new_Button.Width = 180; Location_control.Y += new_Button.Height + 10; //create the event handler //add the new button to the collection of controls Controls.Add(new_Button); } else { //checking if you want to clear the controls that you have added. 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) { Controls.Clear(); //clearing the control Count_control = 0; //returning the count_control to its default value create_button(); //put a control so that you can add another controls } } }
private void myButtonHandler_Click(object sender, EventArgs e) { //verifying the buttons { create_button(); //create a new button } }
private void Form1_Load(System.Object sender, System.EventArgs e) { create_button(); }