Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:34:11+00:00 2026-05-28T17:34:11+00:00

I am dynamically adding and replacing controls in a winform panel at runtime Even

  • 0

I am dynamically adding and replacing controls in a winform panel at runtime

Even though all works I have been told to implement interfaces rather than inheriting from a baseUserControl.

I am all for it but I dont see how I can I achieve the same result using interfaces

How would I code my Factory?

How can this be improved and use interfaces instead ?

//Simplified noddy example

//Client code
var controlA = ControlFactory
    .Create("UserControlA") as UserControlA;

panel1.Controls.Add(ControlA);

//Factory
public class ControlFactory
{
    public static BaseUserControl Create(string name)
    {
        switch (name)
        {
            case "UserControlA":
                var userControlA = new UserControlA();

                return userControlA;

            case "UserControlB":
                var userControlB = new UserControlB();
                return userControlB;
        }
        return null;
    }
}
   //BaseUserControl
   public partial class BaseUserControl : UserControl
    {
        public BaseUserControl()
        {
            InitializeComponent();
        }

        public virtual void DoSomething()
        {

        }
    }

    public partial class UserControlA : BaseUserControl
    {
        public UserControlA()
        {
            InitializeComponent();
        }

        public override void DoSomething()
        {
            //Do something here
        }
    }

public partial class UserControlB : BaseUserControl
{
    public UserControlB()
    {
        InitializeComponent();
    }

    public override void DoSomething()
    {
        //Do something here
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:34:12+00:00Added an answer on May 28, 2026 at 5:34 pm

    Here’s how you can do it:

    using System;
    using System.Windows.Forms;
    using System.ComponentModel;
    
    //Interface
    public interface IControl : IComponent
    {
        void DoSomething();
    }
    
    //Factory
    public class ControlFactory
    {
        public static IControl Create(string name)
        {
            switch (name)
            {
                case "UserControlA":
                    var userControlA = new UserControlA();
    
                    return userControlA;
    
                case "UserControlB":
                    var userControlB = new UserControlB();
                    return userControlB;
            }
            return null;
        }
    }
    
    //BaseUserControl
    public partial class BaseUserControl : UserControl, IControl
    {
        public BaseUserControl()
        {
            InitializeComponent();
        }
    
        public virtual void DoSomething()
        {
    
        }
    }
    
    public partial class UserControlA : BaseUserControl, IControl
    {
        public UserControlA()
        {
            InitializeComponent();
        }
    
        public override void DoSomething()
        {
            //Do something here
        }
    }
    
    public partial class UserControlB : BaseUserControl, IControl
    {
        public UserControlB()
        {
            InitializeComponent();
        }
    
        public override void DoSomething()
        {
            //Do something here
        }
    }
    

    You can retain BaseUserControl if you have any functionality that is common to UserControlA and UserControlB; otherwise, eliminate it and make the latter two derive directly from UserControl.

    You should define all members you might need to access from your derived classes within your IControl interface. This includes any members you will inherit from UserControl. However, you will not need to re-implement these within your concrete classes.

    //Interface
    public interface IControl : IComponent
    {
        void DoSomething();
    
        // To be inherited from UserControl.
        Size Size { get; set; }
        bool Focus();
        event EventHandler FontChanged;
    }
    

    If you need to add these controls to a Windows Forms application – typically as the argument to a Control.ControlCollection.Add method call – you will need to get a Control instance corresponding to your control. Under the current implementation, this may be achieved simply through casting; however, this needs to be insulated from the consumers of your interface, in case you decide to change the underlying implementation in the future. Thus, I would use:

    //Interface
    public interface IControl : IComponent
    {
        void DoSomething();
    
        Control AsWindowsForms();
    }
    
    //BaseUserControl
    public partial class BaseUserControl : UserControl, IControl
    {
        public BaseUserControl()
        {
            InitializeComponent();
        }
    
        public virtual void DoSomething()
        {
    
        }
    
        public Control AsWindowsForms()
        {
            return this as Control;
        }
    }
    

    In your client code:

    var controlA = ControlFactory.Create("UserControlA").AsWindowsForms();
    var controlB = ControlFactory.Create("UserControlB").AsWindowsForms();
    panel1.Controls.Add(controlA);
    panel1.Controls.Add(controlB);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble dynamically adding controls inside an update panel with partial postbacks. I've
I have a WinForms TabControl that I am dynamically adding TabPages to at runtime.
Very weird situation going on with a FlowLayoutPanel... I have been dynamically adding user
I have a form on my page and am dynamically adding controls to the
i have a aspx page on it i'm dynamically adding the web user controls
I have a DataGridView on a winform. I am dynamically adding DatagridViewButtonColumn in the
I have created user control container (panel actually) which dynamically adding and removing user
I have a flowpanel that I'm dynamically adding usercontrols to. I want it to
I have a situation where I am dynamically adding some content to popup box
I have a canvas with a mxml component and I'm dynamically adding components to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.