Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Switching UserControl at runtime
Message
From
03/02/2011 07:07:50
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Forms
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01497625
Message ID:
01498491
Views:
36
>>>>>>>Hi all, I need to be able to dynamically switch between UserControls on a TabPage but am unsure as to how to elegantly achieve this. So far I've tried creating another uc , adding it to the TabPage's control collection and calling the new uc's BringToFront() method. This works but how do I reinstate the original uc ? also I assume the new uc is still in the control collection of the TabPage so how do I reuse it if an when I need to ? or should I remove it ? Hope this makes sense to someone out there - maybe my thought process is screwed ?
>>>>>>
>>>>>>A couple of thoughts:
>>>>>>
>>>>>>1) What you're doing currently is ok. Since you haven't removed the original UserControl, it is still in the TabPage's Controls collection, so you could just use BringToFront() on the original UserControl when you need it. The downside to this is that probably more memory is consumed, having them both loaded.
>>>>>>
>>>>>>2) An alternative is to remove the original UserControl from the Controls collection before or after you add the second UserControl (after's probably better) and do the switching back-and-forth that way. The downside to this is that the UserControl may take awhile to instantiate and reload. This may appear slower to the user.
>>>>>>
>>>>>>3) I think that if there's the possibility that you're going to be switching back-and-forth between those two UserControls more than once, then you might as well leave them both and use option #1. Otherwise, if you're just loading one, then loading the second and that's it, then go with option #2. If you've got more than just two UserControls that you'll be swapping, then definitely go with option #2.
>>>>>
>>>>>If the UserControls are added at design time I don't think they will go out of scope when they are moved from the Controls collection.
>>>>>Why not just remove the first one that is not intially needed in the form constructor:
public Form1()
>>>>>        {
>>>>>            InitializeComponent();
>>>>>            tabPageX.Controls.Remove(UserControlB);
>>>>>         }
>>>>>        // Then to swap:
>>>>>            if (tabPageX.Controls.Contains(UserControlB))
>>>>>            {
>>>>>                tabPageX.Controls.Add(UserControlA);
>>>>>                tabPageX.Controls.Remove(UserControlB);
>>>>>            }
>>>>>            else
>>>>>            {
>>>>>                tabPageX.Controls.Add(UserControlB);
>>>>>                tabPageX.Controls.Remove(UserControlA);
>>>>>            }
>>>>
>>>>
>>>>
>>>>Lots to think over - thanks to you both I will post again when I have it sussed !
>>>
>>>Hello to you both, I eventually decided to create both uc's in the TabPage's ctor and utilize SendToBack in the click event of a button which is on both uc's. Works like a charm so thanks to you both. What I need now is a way to change the TabPage's Text property when switching uc's - I've tried using the OnEnter and OnGotFocus events but they don't fire ( apparently a well known problem with uc's as container controls never really get the focus ! ) can you think of a way around this ?
>>
>>Raise an event in the user control and subscribe to it where the tab control is contained. I have a windows application that is completely driven by user controls and only a single form. I have an event for OnSwitchCompleted that takes care of all things I might need to do when a switch occurs.
>>Tim
>
>
>Hi Tim, thanks for that - so to clarify - I define the delegate/event in the uc and subscribe to it in the TabPage ?

Ok, I just went back and read through your thread. Do you want to change the text at the same time as you bring one control to the front? It looks like you are bringing to the front within a button click. The buttons are on the User Control? Then yes, if you are switching your user controls from within the user controls, then define the event in the UC's and subscribe on the page containing the TabPage. Are both user controls based on the same subclass? I would define the event in the base class and send as part of the eventArgs which control raised the event.
Tim
Timothy Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform