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 7846963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:45:32+00:00 2026-06-02T17:45:32+00:00

Correct way to add and remove user controls on a panel. I have some

  • 0

Correct way to add and remove user controls on a panel.

I have some doubts about this:

  1. It is right (better way) to do it this way?
  2. Leave some waste in memory to run the application after a while?

Currently I have a main window as follows:

<!-- MainWindow.xaml -->
<Window x:Class="Empresas.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Empresas" Height="480" Width="640"
        MinHeight="480" MinWidth="640">
    <DockPanel HorizontalAlignment="Stretch" Width="Auto" Margin="0">

        <!-- Menu -->
        <Menu x:Name="MainWindowClientesMenu" Width="Auto" Height="25"
              DockPanel.Dock="Top">
            <MenuItem Header="_Archivo">
                <MenuItem Header="Agregar _Nueva empresa" x:Name="MainWindowClientesAgregarEmpresa" Click="MainWindowClientesAgregarEmpresa_Click" />
                <Separator/>
                <MenuItem Header="Salir"/>
            </MenuItem>
        </Menu>
        <!-- Fin Menu -->

        <!-- Barra de Estado -->
        <StatusBar x:Name="MainWindowClientesStatusBar" Width="Auto" Height="25"
                   DockPanel.Dock="Bottom" Background="#ddd" HorizontalAlignment="Stretch">
            <StatusBarItem x:Name="MainWindowClientesCurrentAction" HorizontalContentAlignment="Right"
                           Margin="0 0 5 0"></StatusBarItem>
        </StatusBar>


        <StackPanel x:Name="MainWindowClientesContenido"></StackPanel>
        <!-- Fin Lista isquierda de Empresas/Clientes -->

    </DockPanel>
</Window>

My user control is as follows

<!-- NuevaEmpresa.xaml -->
<UserControl x:Class="Empresas.View.NuevaEmpresa"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Height="300" Width="350">
    <Grid>
        <Label Content="Razon Social" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="118,14,0,0" Name="NuevaEmpresaRazonSocialTxtBox" VerticalAlignment="Top" Width="220" />
        <Label Content="Nit" Height="28" HorizontalAlignment="Left" Margin="12,52,0,0" Name="label2" VerticalAlignment="Top" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="118,54,0,0" Name="NuevaEmpresaNitTxtBox" VerticalAlignment="Top" Width="220" />
        <Button Content="Crear Empresa" Height="23" HorizontalAlignment="Left" Margin="223,114,0,0" Name="NuevaEmpresaCrearButton" VerticalAlignment="Top" Width="115" Click="NuevaEmpresaCrearButton_Click" />
        <Button Content="Cancelar" Height="23" HorizontalAlignment="Left" Margin="93,114,0,0" Name="NuevaEmpresaCancelarButton" VerticalAlignment="Top" Width="115" Click="NuevaEmpresaCancelarButton_Click" />
    </Grid>
</UserControl>

and how they add and remove the control is as follows:

// MainWindow.xaml.cs
private void MainWindowClientesAgregarEmpresa_Click(object sender, RoutedEventArgs e)
        {
            MainWindowClientesContenido.Children.Add(new View.NuevaEmpresa(MainWindowClientesContenido));
        }

// NuevaEmpresa.xaml.cs
public partial class NuevaEmpresa : UserControl
{
    // ...
    private Panel _parent;    

    public NuevaEmpresa(Panel parent)
    {
        InitializeComponent();
        _parent = parent;
        // ...

    }

    // ...

    private void NuevaEmpresaCancelarButton_Click(object sender, RoutedEventArgs e)
    {
        _parent.Children.Clear();
    }
}
  • 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-02T17:45:33+00:00Added an answer on June 2, 2026 at 5:45 pm

    I don’t see why this could be wrong. You’re setting its child and then clearing it. It seems pretty sane to me. Also don’t see why memory could be leaked. Once the parents clear its children then it must be disposed.

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

Sidebar

Related Questions

In storyboard, is this the correct way to add a scrollview? drag a view
What would be the correct way to add DISTINCT and/or GROUPBY to ContentResolver -based
I am struggling to find out the syntactically correct way in which to add
What is the correct way to do this? For example, how would I change
I have a listbox that users can add/remove from. To add, there's a text
I am writing an android application where the user can add and remove fields
I'd like to know if this is the correct way to avoid a memory
What is the correct way to write code to do the following: Base64 base64
What is the correct way to import a C++ class from a DLL? We're
What is the correct way to check if a value is a date/number in

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.