samedi 8 août 2015

When setting winform panel name by reflection, setting visible to true fails. Why?

I am attempting to loop through a number of panels which have .Visible = false set by default. I want to change these to true, but I will only know which ones at run time.

I have the following code:

var genericPanel =  new Panel();
            var myName = "panel" + i;
            PropertyInfo prop = genericPanel.GetType().GetProperty("Name", BindingFlags.Public | BindingFlags.Instance);
            if (null != prop && prop.CanWrite)
            {
                prop.SetValue(genericPanel, myName, null);
            }

            genericPanel.Enabled = true;
            genericPanel.Visible = true;
            var blah = genericPanel.Name;  // Name is correct
            Application.DoEvents();

            // This works fine
            //panel1.Visible = true;
            //panel1.Enabled = true;
            //Application.DoEvents();

Using reflection I appear to be able to set the object name correctly, but my attempts at setting the visibility and enabled properties fail. Doing so directly works just fine.

What am I missing?





Aucun commentaire:

Enregistrer un commentaire