473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

User Control Inheritance - Swapping user conrol at runtime

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);

Feb 21 '07 #1
2 5469
On Feb 21, 11:56 am, "wildThought" <andyb...@gmail.comwrote:
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).

Feb 21 '07 #2
On Feb 21, 5:06 pm, "Bruce Wood" <brucew...@canada.comwrote:
On Feb 21, 11:56 am, "wildThought" <andyb...@gmail.comwrote:
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).
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.u o1);
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.

Feb 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Wiebe | last post by:
Hi all Ive been tearing my hair out over this for the last couple of days and just cant get it sorted so any help would be greatly appreciated. I have a single asp.net page that simply has a...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
6
by: Rob Morgan | last post by:
I have a grid and a user control on the same page. The user control has a save button that triggers a click event server side. Once the click event happens the page renders, but I need to update...
0
by: keithb | last post by:
Using Website | ASP.NET Configuration, I set up a role name Administrators and a user named admin. In my application, I put a LoginView Conrol on my master page. The LoginView control has an...
8
by: mark.norgate | last post by:
I've run into a few problems trying to use generics for user controls (classes derived from UserControl). I'm using the Web Application model rather than the Web Site model. The first problem...
11
by: Webbert | last post by:
I am trying to display XML in a WebBrowser Control. I receive a data feed of XML and am trying to inject it into the control. I have not been successful in doing so. The only solution I have...
8
by: | last post by:
I'm looking for some design guidance on a collection of projects I'm working on. The project involves a bunch of websites constructed out of a collection of user controls. Different user...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
1
by: tshad | last post by:
In VB 2008, I have a user control added to the page in the PageLoad event - but the properties are causing me an error. The program (TakeSurveyTest.aspx) using the control (ContactInfo): <%@...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.