Related to: C#; Windows form, Storing Multiple Instances of a User Control:

Hi:

I’ve created an app with a user control that accepts parameters. When a user clicks a button I need to store the current view of the control and bring into focus on demand. Several instances of the UC need to be stored with different parms. Is a generic collection the best way to do this?

Thanks.

Dani AI

Generated

Good follow-up from — using a List<T> to keep instances is a solid start (as suggested) and was right that hiding can avoid disposal. Add a bit more structure so instances stay reusable and you don’t accidentally destroy them when switching views.

Keep the instances in your list but do not call Dispose when you want to reuse a control. Detach it from the container or hide it instead of disposing. Removing a control from a parent’s Controls collection does not automatically free its native resources; you can remove and later re-add the same instance. Example pattern (short):

// keep in cache
cache.Add(myUc);

// show
panel.Controls.Add(myUc);

// hide without disposing
panel.Controls.Remove(myUc);   // does not dispose the control
// or simply
myUc.Visible = false;

// later re-show
panel.Controls.Add(myUc);
myUc.Visible = true;
myUc.BringToFront();

Microsoft’s docs show the add/remove pattern and recommend explicitly calling Dispose only when you truly want to free a control and its handles. (learn.microsoft.com)

If you do need to free a control, unsubscribe any external event handlers first (to avoid leaks), remove it from its parent, then call Dispose and remove it from your list:

panel.Controls.Remove(myUc);
myUc.SomeEvent -= ExternalHandler;
myUc.Dispose();
cache.Remove(myUc);

Dispose releases unmanaged handles and child resources; Clear() or Remove() alone does not. Use Dispose only when you no longer need the instance. (learn.microsoft.com)

Finally, keep UI-thread rules in mind: create/remove/reparent controls on the UI thread (use Invoke/BeginInvoke or the thread-safe patterns in the WinForms docs). If you expect many instances, consider storing only the parameter/state model and recreating controls on demand (or use pooling) to balance memory vs. responsiveness. (learn.microsoft.com)

Recommended Answers

All 4 Replies

Try using a List<T> of the control.

Try using a List<T> of the control.

Thanks. That worked perfectly. Here’s what I did—just to test:
• Created user control with 3 textboxes, and 3 string parameters in the constructor.
• Created a form with a button, tab panel, and List<T> for the user control.
• On form load, created 10 instances of the user control, passing 3 unique string parameters. The user control then set the values in the textboxes. Added each instance to the List<T>.
• The purpose is now to display each instance on demand in the tab panel. The button click event “rotated” thru the List<T> of user control instances and added it to the panel with the textbox values properly displayed. Worked great. Only thing though, I disposed the control (from the tab panel) before adding the next instance. Once disposed, it’s no longer is available in the List<T>. I need all instances kept alive in the background, so I’ll try a different approach when removing from the tab panel.

Thanks Again,
ventura1

Simply adjusting the .Visible property should allow you to hide the controls without disposing of them.

Simply adjusting the .Visible property should allow you to hide the controls without disposing of them.

Yes thanks - I'll use that approach.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.