【发布时间】:2010-07-30 14:24:53
【问题描述】:
也就是说,下面代码中的第 2 行会有什么影响吗?
// let's assume that myForm has not previously been added
myControlCollection.Add(myForm);
myControlCollection.Add(myForm); // line 2
【问题讨论】:
标签: winforms
也就是说,下面代码中的第 2 行会有什么影响吗?
// let's assume that myForm has not previously been added
myControlCollection.Add(myForm);
myControlCollection.Add(myForm); // line 2
【问题讨论】:
标签: winforms
相信立即连续执行两次Add不会有明显效果。但是,如果有其他控件介入 Add 调用,它将 - 因为 Add 更新 Z 顺序,将新添加的控件发送到后面。例如:
using System;
using System.Drawing;
using System.Windows.Forms;
class Test
{
static void Main()
{
Form f = new Form();
Button b1 = new Button
{
Location = new Point(50, 50),
Size = new Size(40, 40),
Text = "b1"
};
Button b2 = new Button
{
Location = new Point(70, 70),
Size = new Size(40, 40),
Text = "b2"
};
f.Controls.Add(b1);
f.Controls.Add(b2);
// f.Controls.Add(b1);
Application.Run(f);
}
}
这会在b2 前面显示b1 - 但如果您取消对Add(b1) 的第二次调用的注释,则顺序将颠倒。
【讨论】:
不,第二行不会有任何影响。该集合不会两次添加相同的控件实例。
【讨论】:
对第二行的执行没有影响。
【讨论】: