Hello,

I'm trying to create a Button Class that will be used to create buttons on a form. This is the class I've created:

public class CreateButton : Button
    {
        public Button newButton;

        private string _buttonName;
        private Point _buttonLocation;
        private Size _buttonSize;

        public string ButtonName
        {
            get { return _buttonName; }
            set {_buttonName = value; }
        }

        public Point ButtonLocation
        {
            get { return _buttonLocation; }
            set { _buttonLocation = value; }
        }

        public Size ButtonSize
        {
            get { return _buttonSize; }
            set { _buttonSize = value; }
        }

        public CreateButton(string btnName, Point btnLocation, Size btnSize)
        {
            ButtonName = btnName;
            ButtonLocation = btnLocation;
            ButtonSize = btnSize;

            newButton = new Button();
            newButton.Text = btnName;
            newButton.Location = btnLocation;
            newButton.Size = btnSize;
            Controls.Add(newButton);
        }
    }

Then... in the main form where I want to create the buttons, I use the following code:

CreateButton btn0 = new CreateButton("Save", new Point(5, 5), new Size(40, 40));
            Controls.Add(btn0);

The thing is that it actually creates two buttons, with the button I want to create inside another one. If I don't add the control no button will be created...
Can anyone please tell me what I'm doing wrong, because I don't really know what I'm doing here :/

Thank you.

nvm... problem solved, modified the class a little and called the class different:

CreateButton btn0 = new CreateButton("New","newFile", new Point(4, 5), new Size(50, 40));
            btn0.newButton.Click += new EventHandler(newButton_Click);
            fileTab.Controls.Add(btn0.newButton);

The code that I will post now, its one hell of a code. I just did it for my 1st time. And its works perfectly. I took me an hours to do it all.
I did as you wanted. On pre-installed button, the code created new buttons (one under another - if you want to position them manally, some changes need to be made for button.Location, but this is simple).
And when clicking on created buttons, you can do what ever you want with the code.

public partial class Form1 : Form
    {
        List<Button> listButtons;
        public Form1()
        {
            InitializeComponent();
            listButtons = new List<Button>();
        }

          private void buttonA_Click(object sender, EventArgs e)
        {
            int sizeX = this.buttonA.Width;
            int sizeY = this.buttonA.Height;
            int locationX = this.buttonA.Location.X;
            int locationY = this.buttonA.Location.Y;
            int i = listButtons.Count;
            foreach (Button _button in listButtons)
            {
                locationY = _button.Location.Y;
            }

            Button button = new Button();
            button.Name = "button" + (i + 1).ToString();
            button.Text = "button" + (i + 1).ToString();
            button.Location = new Point(locationX, (locationY + 30));
            button.Size = new Size(sizeX, sizeY);
            button.Click += new EventHandler(button_Click);
            button.Tag = i + 1;
            this.Controls.Add(button);
            listButtons.Add(button);
        }

         private void button_Click(object sender, EventArgs e)
         {
            int all = listButtons.Count;
            for (int i = 0; i < listButtons.Count; i++)
            {
                Button buttn = sender as Button;
                int index = (int)buttn.Tag;

                //
                //you can choose of 2 types of code (choose one, erase the other option):
                //

                //1: common code:
                if (index == (i+1))
                {
                    //here is a common code for all the buttons
                    //but it is created only by selected button
                    MessageBox.Show("You have pressed button number " + (i + 1).ToString());
                }

                //2. code bellow allows you to do your own code with a specific button:
                //code for button1:                
                if (index == 1)
                {

                }
                //code for button2:
                else if (index == 2)
                {

                }
                //code for button3:
                else if (index == 3)
                {

                }
                //and continue with creating code for other buttons 4th, 5th, ...
            }
         }       
     }

Hope this helps..

I think that what you are doing is better done in a method on the main form.
This is a modified version of your CreateButton constructor as a method.

// Creates a new button with given text, name, location, and size
        // then adds to this form
        public void CreateButton(string btnText, string btnName, Point btnLocation, Size btnSize)
        {
            Button newButton = new Button();
            newButton.Name = btnName;
            newButton.Text = btnText;
            newButton.Location = btnLocation;
            newButton.Size = btnSize;
            Controls.Add(newButton);
        }

If you really want to do this in a class then you only need to have the constructor.
You do not need the properties as these already exist in the Button class you are wrapping.

using System.Windows.Forms;
using System.Drawing;

// button wrapper class to provide additional contructor
class MyButton : Button
{
    // Creates a new button with given text, name, location, and size.
    public MyButton(string btnText, string btnName, Point btnLocation, Size btnSize)
    {
        this.Text = btnText;
        this.Name = btnName;
        this.Location = btnLocation;
        this.Size = btnSize;
    }

    // add any other properties here
}

Which is used just like the normal button but with the extended constructor.

MyButton btn0 = new MyButton("New", "newFile", new Point(4, 5), new Size(50, 40));
            btn0.Click += new EventHandler(newButton_Click);
            Controls.Add(btn0);
            // etc.

The code that I will post now, its one hell of a code. I just did it for my 1st time. And its works perfectly. I took me an hours to do it all.
I did as you wanted. On pre-installed button, the code created new buttons (one under another - if you want to position them manally, some changes need to be made for button.Location, but this is simple).
And when clicking on created buttons, you can do what ever you want with the code.

public partial class Form1 : Form
    {
        List<Button> listButtons;
        public Form1()
        {
            InitializeComponent();
            listButtons = new List<Button>();
        }

          private void buttonA_Click(object sender, EventArgs e)
        {
            int sizeX = this.buttonA.Width;
            int sizeY = this.buttonA.Height;
            int locationX = this.buttonA.Location.X;
            int locationY = this.buttonA.Location.Y;
            int i = listButtons.Count;
            foreach (Button _button in listButtons)
            {
                locationY = _button.Location.Y;
            }

            Button button = new Button();
            button.Name = "button" + (i + 1).ToString();
            button.Text = "button" + (i + 1).ToString();
            button.Location = new Point(locationX, (locationY + 30));
            button.Size = new Size(sizeX, sizeY);
            button.Click += new EventHandler(button_Click);
            button.Tag = i + 1;
            this.Controls.Add(button);
            listButtons.Add(button);
        }

         private void button_Click(object sender, EventArgs e)
         {
            int all = listButtons.Count;
            for (int i = 0; i < listButtons.Count; i++)
            {
                Button buttn = sender as Button;
                int index = (int)buttn.Tag;

                //
                //you can choose of 2 types of code (choose one, erase the other option):
                //

                //1: common code:
                if (index == (i+1))
                {
                    //here is a common code for all the buttons
                    //but it is created only by selected button
                    MessageBox.Show("You have pressed button number " + (i + 1).ToString());
                }

                //2. code bellow allows you to do your own code with a specific button:
                //code for button1:                
                if (index == 1)
                {

                }
                //code for button2:
                else if (index == 2)
                {

                }
                //code for button3:
                else if (index == 3)
                {

                }
                //and continue with creating code for other buttons 4th, 5th, ...
            }
         }       
     }

Hope this helps..

thank you!!!
the second solution solved another problem I encountered.
great work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.