How to remove a column of 1 label and some combo boxes added dynamically

CORNALD ORJIAKO 0 Reputation points
2023-11-06T16:16:17.7833333+00:00

I am creating a Windows Forms App where I have added columns containing 1 label and some combo boxes. However, I need to delete them dynamically. The code I am using to add them is included below. Thank you for your help.

int columnTop = 143;
int columnLeft = 775;
int horizontalSpacing = 135;

ContextMenu contextMenu = new ContextMenu();

//Dictionary to map user controls to their respective context menus         Dictionary<UserControl, ContextMenu> contextMenus = new Dictionary<UserControl, ContextMenu>();          

List<UserControl> existingColumns = new List<UserControl>();

private void addSLTToolStripMenuItem_Click(object sender, EventArgs e)         
{             
//Creating a new instance of SLT custom user control             
UserControl1 SLTColumn = new UserControl1();              

//Creating an instance of context menu for the SLT user control             
ContextMenu contextMenuSLT = new ContextMenu();              

//Associating the context menu with the SLT user control             
SLTColumn.ContextMenu = contextMenuSLT;              

//Adding the user control and its context menu to the dictionary for future reference             contextMenus.Add(SLTColumn, contextMenuSLT);              

//Setting the location for the SLT user control based on the number of existing columns             SLTColumn.Location = new Point(columnLeft, columnTop);              

//Adding the SLT user control to the main form             
this.Controls.Add(SLTColumn);              

//Adding the SLT user control to the list of existing columns             existingColumns.Add(SLTColumn);              

//Setting spacing between the existing column and the SLT column             
columnLeft += horizontalSpacing;         
}

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,907 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,097 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AddWebSolution 171 Reputation points
    2023-11-23T10:44:56.3933333+00:00

    Hi @CORNALD ORJIAKO,
    To dynamically delete the columns containing labels and combo boxes in a Windows Forms application, you can create a method to remove the controls. Here's an example based on your existing code:

    private void RemoveSLTColumn(UserControl column)
    {
        // Remove controls from the form's Controls collection
        this.Controls.Remove(column);
    
        // Remove the column from the existingColumns list
        existingColumns.Remove(column);
    
        // Dispose of the column to release resources (optional)
        column.Dispose();
    }
    
    private void removeSLTToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (existingColumns.Count > 0)
        {
            // Assuming you want to remove the last added SLT column
            UserControl lastSLTColumn = existingColumns[existingColumns.Count - 1];
            RemoveSLTColumn(lastSLTColumn);
        }
        else
        {
            MessageBox.Show("No SLT columns to remove.");
        }
    }
    
    
    

    In this example, I've created a RemoveSLTColumn method that takes a UserControl (representing your SLT column) as a parameter and removes it from both the form's controls collection and the existingColumns list. It also optionally disposes of the control to release resources.

    You can call this method when you want to remove an SLT column, for example, in response to a button click event. Adjust the logic based on your specific requirements.

    Thanks & Best Regards,

    AddwebSolution

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.