在应用程序中,有在运行时制作的面板。为了访问名为 myTestPanel 的面板,我使用了 controls.find,它创建了一个我必须循环访问的数组。有没有更好的办法?我可以按名称公开声明一些面板,以便我可以操纵名称,就好像它们是通过表单设计器添加的一样?
这是我在命名空间内的代码。
private void mnuForm_Shown(object sender, EventArgs e)
{
Panel myTestPanel = new Panel();
myTestPanel.Top = 0;
myTestPanel.Left = 0;
myTestPanel.Width = this.Width;
myTestPanel.Height = this.Height;
myTestPanel.Dock = DockStyle.Fill;
myTestPanel.BackColor = System.Drawing.Color.Gold;
myTestPanel.Name = "myTestPanel";
myTestPanel.Click += new EventHandler(myTestPanel_Click);
Controls.Add(myTestPanel);
myTestPanel.Show();
}
private void myTestPanel_Click(object sender, EventArgs e)
{
var myTestPanel = Controls.Find("myTestPanel", true);
foreach (var ReallyStupidWayToDoThis in myTestPanel)
{
Controls.Remove(ReallyStupidWayToDoThis);
ReallyStupidWayToDoThis.Dispose();
}