Adrian Adrian - 3 years ago 112
C# Question

A simple C# question I hope! Add additional properties to Buttons

hi
on a windows form (not WPF) I dynamically create buttons on a flowlayout and I would like to add some properties to them simply to store other values (int and string) with the buttons for latter use.

Button bn = new Button();
bn.Text = "mybutton";
bn.Name = "mybutton";
toolTip1.SetToolTip(bn, "some tip");
bn.Location = new Point(200, 200);
bn.Size = new Size(110, 30);
bn.BackColor = SystemColors.Control;
bn.Show();
flowLayoutPanel1.Controls.Add(bn);


I have about 6 values I would like to store with each button as it is different for each button..

Can this be done?

Answer Source

Derive from Button:

public class MyButton : Button
{
  public string ExtraProperty {get;set;}
}

Personally, I think this is bad code. Really bad code.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download