User Control Inheritance - Swapping user conrol at runtime

W

wildThought

I have a window with a user control on it. I want to be able to swap
out different user controls depending on a menu choice. The swappable
user controls UserControlBaseA and UserControlBaseB are inherited
from UserControlBase. What I did is I placed UserControlBase on my
window and it has a name of uc1. Now when the window loads or if the
user chooses a different user control from the menu I want to change
the assignment of uc1.

The following compiles, yet I cannot see the new user control on the
window. Any suggestions?

uo1 = new ucUserControlBaseA();
uo1.Show();
uo1.Size = new System.Drawing.Size(469, 602);
uo1.Location = new System.Drawing.Point(4, 4);
uo1.Invalidate(true);
 
B

Bruce Wood

I have a window with a user control on it. I want to be able to swap
out different user controls depending on a menu choice. The swappable
user controls UserControlBaseA and UserControlBaseB are inherited
from UserControlBase. What I did is I placed UserControlBase on my
window and it has a name of uc1. Now when the window loads or if the
user chooses a different user control from the menu I want to change
the assignment of uc1.

The following compiles, yet I cannot see the new user control on the
window. Any suggestions?

uo1 = new ucUserControlBaseA();
uo1.Show();
uo1.Size = new System.Drawing.Size(469, 602);
uo1.Location = new System.Drawing.Point(4, 4);
uo1.Invalidate(true);

Two suggestions.

First, the usual approach to this problem is to put _all_ of the user
controls on your form, and then at run time just .Show() and .Hide()
them (or .Visible = true or false) as appropriate. I use this for a
wizard-style application that I wrote and it's much easier.

However, if you _must_ add and remove the user controls at run time,
you have to .Add() and .Remove() them from the .Controls collection of
the parent (container) control in which you want them to show up.
Simply creating them and setting their location isn't sufficient: they
have to have a parent somewhere on the Form. Look at the code
generated by the Designer to see how this is done (the Add part,
anyway).
 
W

wildThought

Two suggestions.

First, the usual approach to this problem is to put _all_ of the user
controls on your form, and then at run time just .Show() and .Hide()
them (or .Visible = true or false) as appropriate. I use this for a
wizard-style application that I wrote and it's much easier.

However, if you _must_ add and remove the user controls at run time,
you have to .Add() and .Remove() them from the .Controls collection of
the parent (container) control in which you want them to show up.
Simply creating them and setting their location isn't sufficient: they
have to have a parent somewhere on the Form. Look at the code
generated by the Designer to see how this is done (the Add part,
anyway).

Thank you for your help. I find adding extra controls to a form
cheesy for lack of a better word. I was able to implement what you
said and am posting the after code for posterity:



{

this.splitContainer1.Panel1.Controls.Remove(this.uo1);
this.Controls.Remove(uo1);
if (cType == CorrelationType.MarketData)
{
this.uo1 = new uoCorrelationProperties();
this.Controls.Add(uo1);
this.splitContainer1.Panel1.Controls.Add(this.uo1);
this.splitContainer1.SplitterDistance = 487;
uo1.Size = new System.Drawing.Size(469, 602);
uo1.Location = new System.Drawing.Point(4, 4);
}
else if (cType == CorrelationType.Forward)
{
this.uo1 = new uoForwardCorrelationProperties();
this.Controls.Add(uo1);
this.splitContainer1.Panel1.Controls.Add(this.uo1);
this.splitContainer1.SplitterDistance = 290;
uo1.Size = new System.Drawing.Size(469, 602);
uo1.Location = new System.Drawing.Point(4, 4);
}

uo1.LoadUO();
splitContainer1.SendToBack();
uo1.BringToFront();

The bring to front is crucial. It will not work without it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top