Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Switching UserControl at runtime
Message
From
28/01/2011 17:51:44
 
 
To
28/01/2011 17:17:47
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:
01497720
Views:
42
>>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.
>
>Pete said he was adding to the TabPage programmatically, but I don't know how he instantiated them in the first place.

But even if they were instantiated programatically, all that is required is a reference that is locally scoped to the form....

>
>
>>>>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);
>>            }
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform