473,796 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 UserControlBase A and UserControlBase B 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 ucUserControlBa seA();
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 5488
On Feb 21, 11:56 am, "wildThough t" <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 UserControlBase A and UserControlBase B 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 ucUserControlBa seA();
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...@cana da.comwrote:
On Feb 21, 11:56 am, "wildThough t" <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 UserControlBase A and UserControlBase B 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 ucUserControlBa seA();
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.splitConta iner1.Panel1.Co ntrols.Remove(t his.uo1);
this.Controls.R emove(uo1);
if (cType == CorrelationType .MarketData)
{
this.uo1 = new uoCorrelationPr operties();
this.Controls.A dd(uo1);
this.splitConta iner1.Panel1.Co ntrols.Add(this .uo1);
this.splitConta iner1.SplitterD istance = 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 uoForwardCorrel ationProperties ();
this.Controls.A dd(uo1);
this.splitConta iner1.Panel1.Co ntrols.Add(this .uo1);
this.splitConta iner1.SplitterD istance = 290;
uo1.Size = new System.Drawing. Size(469, 602);
uo1.Location = new System.Drawing. Point(4, 4);
}

uo1.LoadUO();
splitContainer1 .SendToBack();
uo1.BringToFron t();

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
1544
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 placeholder on it this page loads a control that has placeholders on it into which are injected user controls depending on a database table one of these controls is a menu that contains a drop down list box with a
1
6287
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 variable (targetId) in to the usercontrol (IntergySite.aspx) by calling its setter method. Currently, I am using if-then-else and hardcoded the User Control Object to do casting and call the setter method. Question: Is there any way I could load,...
6
1386
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 the grid information before it renders. Once a conrol's event finishes is there a way to run a method on the parent page so I can update the gid? Any thoughs on how I might accomplish this?
0
990
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 anonymous Template, and a <RoleGroup> with Roles = "Administrators" The login accepts the credentials of admin when they are entered correctly; however the status of the LoginView conrol continues to say "You are not logged in." Can someone...
8
3191
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 I'm having is that the partial class signature in my projectDetails.ascx.cs file looks like this: public partial class ProjectDetailsControl<TEntryServiceProvider: UserControl, INamingContainer where TEntryServiceProvider : IEntryServiceProvider...
11
8722
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 found is to write it to a temp file and then use the Navigate method to load it. As the control is capable of loading it from disk, I would like to find a way to skip the save/load and just inject. Thanks, Dave
8
1703
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 populations with different access rights and "roles" will be visiting the site. I will be using ASP.NET 2.0's membership, roles, and profiles stuff to manage access. User controls need to be visible or not visible depending on user role. In some...
23
4616
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
1899
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): <%@ Page Language="VB" AutoEventWireup="true" Trace="true" CodeFile="TakeSurveyTest.aspx.vb" Inherits="TakeSurveyTest" %> .... <div visible="true" runat="server"> <asp:PlaceHolder ID="ContactInfo" runat="server"/<br />
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10244
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10201
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.