Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm new to c# and i would like to make a windows form that consist of a NumericUpDown and a button for add/delete user control,
i was able to add one user control with the below code

C#
private void btnCounter_Click(object sender, EventArgs e)
        {
            UserControlLibelle myUserControl = new UserControlLibelle();
            myUserControl.Location = new Point(iPositionInitiale_X, iPositionInitiale_Y + (1 * iEcartEntre2UserCOntrols));
            myUserControl.Name = "UserControl_100";
            myUserControl.Size = new System.Drawing.Size(284, 161);
            myUserControl.TabIndex = 100;
            this.Controls.Add((myUserControl));
        }


can you please help me to add/delete several user control,

thanks in advance
Posted
Comments
BillWoodruff 12-Dec-13 6:49am    
What is the relationship of the NumericUpDown Control to your UserControls ? Does it specify the number of the UserControl you want deleted if you Click the delete button ?

Please give more detail.
Member 10414026 12-Dec-13 7:09am    
The numericUpDown is just for help the user choose how many usercontrols he will add or delete and after that he need to click the button,
thank you
BillWoodruff 12-Dec-13 8:48am    
So, if the user sets the value of the NumericUpDown to 5, and there are 10 UserControls, and the user hits the Delete Button: what determines which UserControls get deleted ?
Sadique KT 12-Dec-13 7:19am    
Add dock panel then add usercontrol to the panel

1 solution

Here's a general solution of adding and deleting UserControls based on the current value of a NumericUpDown Control. There are two Buttons, btnAdd, and btnDelete.

You can modify the code in the loops inside the Click EventHandlers for the two Buttons to do what you want to do:
C#
private int nOfUserControls;

private void btnAdd_Click(object sender, EventArgs e)
{
    for (int i = 0; i < nOfUserControls; i++)
    {
        AUserControl newUserControl = new AUserControl();

        // set the UserControl instance Button's Text
        newUserControl.ucButton.Text = "button " + i.ToString();

        // set the UserControl Location
        // using the loop index variable
        // to position the UserControl vertically
        newUserControl.Location = new Point(10, ((i + 1) * 40));

        // add the new UserControl to the Panel's
        // Control Collection
        pnlUCContainer.Controls.Add(newUserControl);
    }
}

private void btnDelete_Click(object sender, EventArgs e)
{
    // avoid out-of-range error
    if (nOfUserControls > pnlUCContainer.Controls.Count)
    {
        nOfUserControls = pnlUCContainer.Controls.Count;
        numericUpDown1.Value = nOfUserControls;
    }

    for (int i = 0; i < nOfUserControls; i++)
    {
        pnlUCContainer.Controls.RemoveAt(0);
    }
}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    nOfUserControls = Convert.ToInt32(numericUpDown1.Value);
}
The very simple UserControl used here has only a Button on it:
C#
public partial class AUserControl : UserControl
{
    public AUserControl()
    {
        InitializeComponent();
        ucButton = button1;
    }

    // expose the Button Control so creators
    // of the Control can set its Properties
    // or EventHandlers
    public Button ucButton { get; private set; }
}
 
Share this answer
 
Comments
Member 10414026 13-Dec-13 2:49am    
Thanks a lot Bill,

Can you please advise whats is pnlUCContainer
and the button1 ?

thanks again for your help
WuRunZhe 13-Dec-13 4:33am    
Well, pnlUCContainer is the panel which is located on you main Form.
The button1 is the button which is positioned at your newly made user control.
WuRunZhe 13-Dec-13 4:35am    
Very simple and correct.
Member 10414026 13-Dec-13 4:58am    
Thanks WuRunZhe for the clarification,
i asked the questions above as i have a problem with the deletion,
when i click on the delete button,
its delete the buttons and the label but not the user control
however the add button its work perfect,
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900