views:

2197

answers:

3

I am working in a C# winforms project and I have a user control which gets loaded upon its selection from a tool-strip menu. I have a dictionary lookup set to occur upon form load of the user control for other functionality. Also, when I close the user control I am just using the ".Hide();" method. I noticed that when I load the user control the first time everything is fine, but when I close it and choose to open it again the 2nd time it creates a new instance of the object thus throwing off my dictionary lookup. Therefore, I wrote some code in an attempt to fix the issue.

What I need to do is to somehow say that if an instance of the user control already exists, do not create a new instance of that object. Instead, just make the user control visible again. Therefore I have written code in an attempt to accomplish this purpose. When I select the item the first time, everything is fine. When I hide the user control and try to re-open it again nothing happens.

The following is the code I have written for this purpose which occurs upon the selection of the item from the tool-strip menu:

      if (Controls.ContainsKey("CheckAvailUserControl"))
       {
           Controls["CheckAvailUserControl"].Dock = DockStyle.Fill;
           Controls["CheckAvailUserControl"].Visible = true;
           Controls["CheckAvailUserControl"].Show();
           Controls["CheckAvailUserControl"].Refresh();
       }

       else
       {
          UserControl checkAvailUserControlLoad = new CheckAvailUserControl();
          Controls.Add(checkAvailUserControlLoad);
          checkAvailUserControlLoad.Dock = DockStyle.Fill;
          checkAvailUserControlLoad.Visible = true;
          checkAvailUserControlLoad.Show();
       }

When I trace through my code in the debugger it is in fact hitting the right parts of the above if/else statement. Its just not displaying the user control on the screen the 2nd time I attempt to load it.

The question is: How do I get the user control to load correctly after I close it, then select it from the tool-strip menu again?

+7  A: 

I think that Controls.ContainsKey(...) is always returning false, because you never assigned a name to your control when you created it.

If, when you create the control, you say

//...
checkAvailUserControlLoad.Name = "Something"
//...
Controls.Add(checkAvailUserControlLoad);

then

Controls.ContainsKey("Something")

will return true, and you'll be able to re-use the control by using Controls["Something"]

Daniel LeCheminant
Yes, unless the code setting the Name property is omitted.
Henk Holterman
@Henk: Isn't that the whole point of my post, that you have to set the Name property?
Daniel LeCheminant
+1  A: 

This could work, but I think it's a little bit backwards: you're throwing new code at a problem that could be solved instead by moving your old code.

Instead, think about how the events work in your form. I bet that if you move your creating code to a slightly different event, or detect when the event is fired later and ignore those, you could fix the problem in a much nicer way.

Joel Coehoorn
A: 

Here you go:

private void button_Click(object sender, EventArgs e)
{
    // pass in the containing panel
    LoadControl<MyControls.MyControl>(panelContainer);
}

void LoadControl<T>(Panel panel) where T : Control, new()
{
    T _Control = GetControl<T>(panel);
    if (_Control == null)
    {
        _Control = new T();
        _Control.Dock = DockStyle.Fill;
        panel.Controls.Add(_Control);
    }
    _Control.BringToFront();
}

T GetControl<T>(Panel panel) where T : Control
{
    Type _Type = typeof(T);
    String _Name = _Type.ToString();
    if (!panel.Controls.ContainsKey(_Name))
        return null;
    T _Control = panel.Controls[_Name] as T;
    return _Control;
}
JerryNixon