Nik
Nik

Reputation: 7273

Dynamically-Added Custom User Controls Not Visible

I have a form in C# with a drop down box. When that drop down box gets changed, I want it to load a custom user control into a panel. I get no compile errors, no runtime errors, but I also get no visible user control. The idea here is that each user control understands the configuration for a different scenario. I want to encapsulate each scenario in its own control and the drop down box allows the user to select the scenario, which loads the control for the user to perform configuration. By adjusting the options in the drop down, I can customize what scenarios a given customer has. As I said, my problem is I cannot get any of the controls to become visible. This is the code I am using for the drop down index change event handler:

private void ddlCollectorType_SelectedIndexChanged (object sender, EventArgs e)
{
    m_currentControl = null;
    pnlDeviceConfig.Controls.Clear ();

    switch ((string) ddlCollectorType.SelectedItem)
    {
        case "SEL-421":
            SEL421ASCIIControl s421 = new SEL421ASCIIControl (this);
            m_currentControl = s421;
            pnlDeviceConfig.Controls.Add (s421);
            break;
        case "SEL-421 (FTP)":
            break;
        case "GE D60":
            GED6061850Control geD60 = new GED6061850Control (this);
            m_currentControl = geD60;
            pnlDeviceConfig.Controls.Add (geD60);
            break;
        case "GE D60 (TFTP)":
            break;
        case "MiCOM P442":
            break;
    }
}

I've only created a couple of the user controls so far, hence the empty case statements. When I make selections that should show me something, I get nothing (confirmed in the debugger that I am hitting the case statement body). Any help will be greatly appreciated!

Upvotes: 2

Views: 5957

Answers (2)

Uwe Keim
Uwe Keim

Reputation: 40726

To follow up on my comment, I'm guessing that the position and/or dimensions of your control are not set.

Try something like:

...
SEL421ASCIIControl s421 = new SEL421ASCIIControl (this);
m_currentControl = s421;
pnlDeviceConfig.Controls.Add (s421);

// TODO: Set real size and position.
s421.Left = 0;
s421.Top = 0;
s421.Width = 100;
s421.Height = 50;

break;
...

You also may use the Dock and Anchor properties of your control.

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

1 You can replace your Menu with PlaceHolder, he is ideal control for adding controls

here an example : http://www.developerfusion.com/code/3826/adding-controls-to-placeholders-dynamically/

2 And for your case you can try to adjust visible property of panel to true

Upvotes: 0

Related Questions