-1

我想知道如何保留创建的最后一个按钮的位置或靠近面板末端的位置,以便在它之后添加一个。此外,我希望这个新按钮在其名称中采用最后一个索引之后的索引,如果再次单击该按钮,则名称将为 addstrat_3

我的代码是:

    private void addstrat_i_Click(object sender, EventArgs e)
    {
        panel3strat.Width += 200;
        Button addstrat_2 = new Button();
        panel3strat.Controls.Add(addstrat_2); 
        addstrat_2.Size = new Size(210, 41);
        addstrat_2.Location = new Point(31,89);
        addstrat_2.Visible = true;   
     }

在这种情况下,我将按钮放在命名位置,但我只想在最后一个下方创建它并使用新名称。

任何想法 ?

谢谢

4

3 回答 3

1

您可以只使用简单的按钮对象列表并跟踪它

List<Button> btlist= new List<Button>()
于 2013-04-29T16:10:25.197 回答
0
private Button lastAddedBtn = null;

private void addstrat_i_Click(object sender, EventArgs e)
{
    panel3strat.Width += 200;
    Button addstrat_2 = new Button();

    addstrat_2.Size = new Size(210, 41);
    addstrat_2.Location = new Point(31,89);
    addstrat_2.Visible = true;  

    panel3strat.Controls.Add(addstrat_2);
    lastAddedButton = addstrat2;

}

你可以做这样的事情,将最后添加的按钮的引用存储在私有字段中。然后,您可以从其他地方访问其属性。

于 2013-04-29T16:10:40.890 回答
0

将您的按钮封装在一个为您处理定位的类中。查看StackPanel并尝试创建类似的东西。

于 2013-04-29T16:11:23.550 回答