c#在表单上添加/删除创建的用户控件 [英] c# Add/Delete Created User Controls on a form

查看:89
本文介绍了c#在表单上添加/删除创建的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是c#的新手,我想制作一个包含NumericUpDown和添加/删除用户控件按钮的窗体,

i能够使用以下代码添加一个用户控件



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

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

推荐答案

这是根据NumericUpDown控件的当前值添加和删除UserControl的一般解决方案。有两个按钮,btnAdd和btnDelete。



你可以修改Click EventHandlers里面循环中的代码,让两个按钮做你想做的事:
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:
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);
}

这里使用的非常简单的UserControl只有一个Button:

The very simple UserControl used here has only a Button on it:

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; }
}


这篇关于c#在表单上添加/删除创建的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆