0

我创建了一个带有“添加”按钮和“删除”按钮的面板。如果您单击“添加”,则会在此标题正下方创建一个面板,您可以根据需要创建任意数量的面板,它们会被列出。

在每个面板上都有一个复选框,如果单击删除按钮后选中该复选框,我想删除该面板。

我可以得到循环的直觉:for,但仍然太新手,无法在没有一点提示的情况下完成此操作。

-

public partial class Test_auto : Form
{
    ArrayList strategyFutureList = new ArrayList();

    public Test_auto()
    {
        InitializeComponent();
        Instance = this;
    }

    //Create a new future strategy
    public void CreateStrategyFuture()
    {
        ConsoleStrategyItem strategyItemFuture = new ConsoleStrategyItem();
        strategyItemFuture.Location = new Point(3, 3);
        futureContainer.Height += 85;
        strategyFutureList.Add(strategyItemFuture);
        futureContainer.Controls.Add(strategyItemFuture);
        ConsoleStrategyItem.Instance.txtStrategyName.Text = "Strat Future " + strategyFutureList.IndexOf(strategyItemFuture) + " ";
        ConsoleStrategyItem.Instance.Name = "strategyFuture" + strategyFutureList.IndexOf(strategyItemFuture);
        ConsoleStrategyItem.Instance.cbxDeleteStrategy.Name = "cbxDeleteFuture" + strategyFutureList.IndexOf(strategyItemFuture);
    }

    //Makes it appear
    private void btnAddStrategyFuture_Click_1(object sender, EventArgs e)
    {
        CreateStrategyFuture();
    }

    //Delete a-some selected strategies
    public void DeleteStrategyFuture()
    {
        for (int i = 0; i < strategyFutureList.Count; i++)
        {
            if (ConsoleStrategyItem.Instance.cbxDeleteStrategy.Checked = true)
            {
            }
        }
    }

    private void btnDeleteStrategyFuture_Click(object sender, EventArgs e)
    {
        DeleteStrategyFuture();
    }
}
4

1 回答 1

0

您不必创建单独的 ArrayList 来维护要添加到的 UserControls 列表futureContainer,您可以简单地遍历它的 Controls 集合。

只需在您的DeleteStrategyFuture()方法中执行此操作

public void DeleteStrategyFuture()
{
    var length=futureContainer.Controls.Count;
    foreach(int i=0; i< length; i++)
    {
        if(futureContainer.Controls[i].GetType()==typeof(ConsoleStrategyItem))
        { 
           bool isChecked =((ConsoleStrategyItem)futureContainer.Controls[i])
                               .Instance.cbxDeleteStrategy.Checked;
           if(isChecked)
           {
               futureContainers.Controls.Remove(futureContainers.Controls[i]);
           }
        }
     }
}
于 2013-05-07T08:20:35.523 回答