资源简介
操作程序窗体的controls属性实现动态添加和删除程序窗体中的控件。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender EventArgs e)
{//动态添加控件
System.Windows.Forms.TextBox textBox2;
System.Windows.Forms.Label label1;
System.Windows.Forms.TextBox textBox1;
System.Windows.Forms.Label label2;
label1 = new System.Windows.Forms.Label();
textBox1 = new System.Windows.Forms.TextBox();
label2 = new System.Windows.Forms.Label();
textBox2 = new System.Windows.Forms.TextBox();
// label1
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(27 29);
label1.Name = “label1“;
label1.Size = new System.Drawing.Size(65 12);
label1.TabIndex = 0;
label1.Text = “用户名称:“;
// textBox1
textBox1.Location = new System.Drawing.Point(98 20);
textBox1.Name = “textBox1“;
textBox1.Size = new System.Drawing.Size(265 21);
textBox1.TabIndex = 1;
//textBox2
textBox2.Location = new System.Drawing.Point(98 47);
textBox2.Name = “textBox2“;
textBox2.Size = new System.Drawing.Size(265 21);
textBox2.TabIndex = 3;
// label2
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(27 56);
label2.Name = “label2“;
label2.Size = new System.Drawing.Size(65 12);
label2.TabIndex = 2;
label2.Text = “用户密码:“;
Controls.Add(label1);
Controls.Add(label2);
Controls.Add(textBox1);
Controls.Add(textBox2);
}
private void button2_Click(object sender EventArgs e)
{//动态删除控件
for (int j = 0; j < this.Controls.Count; j++)
{//注意:Controls中的控件数是动态变化的
foreach (Control MyControl in this.Controls)
{
if (MyControl is Label)
this.Controls.Remove(MyControl);
if (MyControl is TextBox)
this.Controls.Remove(MyControl);
}
}
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 946 2006-05-30 15:27 B002\WindowsApplication1.sln
..A..H. 14848 2006-07-04 00:03 B002\WindowsApplication1.suo
文件 2774 2006-07-03 23:47 B002\WindowsApplication1\Form1.Designer.cs
文件 2666 2006-07-03 23:48 B002\WindowsApplication1\Form1.cs
文件 5814 2006-07-03 23:47 B002\WindowsApplication1\Form1.resx
文件 478 2006-05-30 15:27 B002\WindowsApplication1\Program.cs
文件 3245 2006-05-30 15:28 B002\WindowsApplication1\WindowsApplication1.csproj
文件 1210 2006-05-30 15:27 B002\WindowsApplication1\Properties\AssemblyInfo.cs
文件 2892 2006-05-30 15:27 B002\WindowsApplication1\Properties\Resources.Designer.cs
文件 5612 2006-05-30 15:27 B002\WindowsApplication1\Properties\Resources.resx
文件 1102 2006-05-30 15:27 B002\WindowsApplication1\Properties\Settings.Designer.cs
文件 249 2006-05-30 15:27 B002\WindowsApplication1\Properties\Settings.settings
文件 5632 2005-11-11 22:25 B002\WindowsApplication1\bin\Debug\WindowsApplication1.vshost.exe
文件 20480 2006-07-03 23:48 B002\WindowsApplication1\bin\Release\WindowsApplication1.exe
文件 24064 2006-07-03 23:48 B002\WindowsApplication1\bin\Release\WindowsApplication1.pdb
文件 5632 2005-11-11 22:25 B002\WindowsApplication1\bin\Release\WindowsApplication1.vshost.exe
文件 368 2006-07-04 00:03 B002\WindowsApplication1\obj\WindowsApplication1.csproj.FileList.txt
文件 180 2006-07-03 23:47 B002\WindowsApplication1\obj\Release\WindowsApplication1.Form1.resources
文件 180 2006-05-30 15:28 B002\WindowsApplication1\obj\Release\WindowsApplication1.Properties.Resources.resources
文件 842 2006-07-03 23:47 B002\WindowsApplication1\obj\Release\WindowsApplication1.csproj.GenerateResource.Cache
文件 20480 2006-07-03 23:48 B002\WindowsApplication1\obj\Release\WindowsApplication1.exe
文件 24064 2006-07-03 23:48 B002\WindowsApplication1\obj\Release\WindowsApplication1.pdb
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\obj\Debug\TempPE
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\obj\Release\TempPE
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\bin\Debug
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\bin\Release
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\obj\Debug
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\obj\Release
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\Properties
..AD... 0 2007-11-23 13:24 B002\WindowsApplication1\bin
............此处省略6个文件信息
评论
共有 条评论