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

  • Home
  • SEARCH
  • 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 8722077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:19:45+00:00 2026-06-13T07:19:45+00:00

I have a form and multiple controls. Say I have a single form and

  • 0

I have a form and multiple controls.

Say I have a single form and it should contain some controls.

I’m trying to work with a single form and add and remove controls depending on some use cases.

For example, this is a sample code in my form class:

private void AddToControlPanel(UserControl control)
        {
            this.panelControl.Controls.Add(control);
            this.panelControl.Tag = control;
            this.panelControl.Size = control.Size;
            this.panelControl.Dock = DockStyle.Fill;
            control.Show();
        }

Above sample adds a control to some panel and shows it.

Problem is I can’t manage to add some other control and drop current one. For example, if some login control authenticates successfully, drop login control itself and show other like a search box control.

How can achieve that?

This is what I’ve tried so far:

if (LoginOK)
{
   //Add the next Control I want to load
   this.ParentForm.Controls.Add(new CtlPedirDevolucion());
   //Remove this actual control
   this.ParentForm.Controls.Remove(this);
}

… so later show latest added control using an event handler:

private void FirstForm_ControlRemoved(object sender, ControlEventArgs e)
{
    this.Controls[this.Controls.Count - 1].Show();
}

But this isn’t working for me. In addition I don’t like my approach.

Will you suggest me a better way of achieving this please?

  • 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-06-13T07:19:46+00:00Added an answer on June 13, 2026 at 7:19 am

    I have tried something similar and it works.

    Code in your Form:

    public void SwitchControls(Control removeCtrl, Control addControl)
    {
         panel1.Controls.Remove(removeCtrl);
         panel1.Controls.Add(addControl);
    }
    

    Code in your Control:

    MyForm frm = (MyForm)this.FindForm();
    CtlPedirDevolucion newCtrl = new CtlPedirDevolucion();
    newCtrl.Dosck = DockStyle.Fill;
    frm.SwitchControls(this, newCtrl);
    

    EDIT: My bad, there is no point creating a CtlPedirDevolucion in control that will be removed. It should be instantiated in your Form and there should also be a reference to it. Therefore SwitchControls should have a more complex logic, and maybe more parameters but this is something you can work with. Maybe you could make a switch-case or something that will create a control within your form. I remind you that this is just an example that works but is very bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form with multiple TextBox controls. Some have multiline set to true
I have a Form with multiple different controls like ComboBox , TextBox and CheckBox
I have a form with multiple dropdowns consisting of multiple values. I'm trying to
I have inserted this form with multiple checkbox items which collects some values, What
I have a form with multiple controls, tey are all passed with $_POST and
In a form I have multiple group of controls which are grouped using validation
I have a windows form with multiple controls and OK and Cancel buttons. In
I have a form with multiple fields that I'm validating (some with methods added
I have a form that will multiple Panel controls stacked on top of each
I have a main form with a tab control containing multiple subforms. I need

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.