Share via


C# user control not displaying in panel

Question

Monday, April 6, 2009 2:07 PM

Hi Guys,

I am trying to add a user control to a panel.
The problem is that it's not displying in the panel. Here is my code.

1) I have made a base User Control

 

public partial class BaseUserControl : UserControl
{
         public BaseUserControl(MainForm mainForm) : base()
         {
                 InitializeComponent();
         }
}

**2) My code to load the user control
**
To this method I pass in other user controls that derive from BaseUserControl.

 

private void LoadControl(BaseUserControl control)
{
         control = new BaseUserControl(this);
         control.Dock = DockStyle.Fill;
         pnlContent.Controls.Clear();
         pnlContent.Controls.Add(control);
}

 

 

Any ideas why pnlContent is empty?

Thanks

 

All replies (4)

Monday, April 6, 2009 2:49 PM âś…Answered

Try this:

private void btnNext_Click(object sender, EventArgs e)
{
    currentPosition++;

    switch (currentPosition)
    {
        case 0:
            LoadControl(new UCMain(this));
            btnBack.Enabled = false;
            btnNext.Enabled = true;
            break;
        case 1:
            LoadControl(new UCSelection(this));
            btnBack.Enabled = true;
            btnNext.Enabled = true;
            break;
        case 2:
            LoadControl(new UCConfirm(this));
            btnBack.Enabled = true;
            btnNext.Enabled = true;
            break;
        case 3:
            LoadControl(new UCProgress(this));
            btnBack.Enabled = true;
            btnNext.Enabled = true;
            break;
        default:
            break;
    }
}

private void LoadControl(BaseUserControl control)
{
    control.Dock = DockStyle.Fill;
    pnlContent.Controls.Clear();
    pnlContent.Controls.Add(control);
}

Your problem was in this line:

control = new BaseUserControl(this);

You were creating a new BaseUserControl... regardless of what you received by parameter. I guess that the BaseUserControl is in fact empty./* No comments */


Monday, April 6, 2009 2:23 PM

Well, first of all, it seems pretty pointless to have your LoadControl method receive the BaseUserControl parameter, since it instanciates it in the first line, hence never using it.

Other than that, your code should work. So, maybe if you post the Form's code, or at least where you call the LoadControl method, we could help you./* No comments */


Monday, April 6, 2009 2:40 PM

Here is the code part in my form -

public UCMain main;
public UCSelection selection;
public UCConfirm confirm;
public UCProgress progress; //Each of these derive from BaseUserControl

 

private void btnNext_Click(object sender, EventArgs e)
{
        currentPosition++;

        switch (currentPosition)
        { 
                case 0:
                      LoadControl(main);
                      btnBack.Enabled = false;
                      btnNext.Enabled = true;
                      break;
                case 1:
                      LoadControl(selection);
                      btnBack.Enabled = true;
                      btnNext.Enabled = true;
                      break;
                case 2:
                      LoadControl(confirm);
                      btnBack.Enabled = true;
                      btnNext.Enabled = true;
                      break;
                case 3:
                      LoadControl(progress);
                      btnBack.Enabled = true;
                      btnNext.Enabled = true;
                      break;
                default:
                      break;
        }
}


Monday, April 6, 2009 3:04 PM

Thanks allot!

That did the trick. I didn't realise I was creating a new BaseUserControl every time.