I've been working on a project for a few days now and have hit a part of the project that I don't know how to fix. I have a propertygrid control on the form and buttons that create controls when pressed.

Here's how I am creating the controls:

private void button1_Click(object sender, EventArgs e)
        {
            Button homebtn = new Button();
            homebtn.Text = "New Button";
            homebtn.Location = new Point(20, 20);
            homebtn.Size = new System.Drawing.Size(75, 23);
            homebtn.Name = "Control" + Convert.ToString(controlnum);
            controlnum= controlnum+1;
            this.panel1.Controls.Add(homebtn);
            homebtn.Click += new System.EventHandler(this.SelectEditableControl);
            homebtn.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MoveControl);
            homebtn.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ControlMouseDown);
            
        }

Then when the user clicks on the control it brings up a list of properties in the property grid using the method SelectEditableControl. All created controls use this method.

public void SelectEditableControl(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = sender;
            propertyGrid1.Refresh();
        }

The problem is when you have created multiple controls . It won't change objects it just stays on the current object. I was thinking about setting the propertygrid's selectedobject to null but this didn't work. Thanks in advance for any help.

I changed your code so that you can actually click on the other buttons.

public partial class Form1 : Form
    {
        private int controlnum = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Button homebtn = new Button(); 
            this.panel1.Controls.Add(homebtn);
            homebtn.Text = "New Button" + controlnum.ToString();
            homebtn.Location = new Point( 20, controlnum * 23); 
            homebtn.Size = new System.Drawing.Size(175, 23); 
            homebtn.Name = "Control" + controlnum.ToString(); 
            controlnum = controlnum + 1; 
            homebtn.Click += new System.EventHandler(this.SelectEditableControl); 
        }
        public void SelectEditableControl(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = sender;
            propertyGrid1.Refresh();
        }
    }

// Jerry

that wasn't a problem in my project seeing as all the controls are movable, but that could be a problem for others. =) The problem actually was with the custom control I was using. The dll I downloaded from Codeproject was apparently broken, but a fix was available

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.