标签:
仿照窗体应用程序编写:
任务一:生成一个Form类的窗体对象frm
using System.Windows.Forms; //using指令使用Form对象创建所需的命名空间
//如果using指令不成功,则应该去添加引用,如图
using System.Drawing;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form //Form是空白窗体
{
//Form1继承于空白窗体,又通过构造函数添加新的控件button等
public Form1() //构造函数
{
InitializeComponent(); //初始化函数
}
private void InitializeComponent() //初始化函数
{
this.textBox1 = new System.Windows.Forms.TextBox(); //创建新的textbox1控件
this.SuspendLayout(); //挂起控件更新,使得以后一同更新
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(108, 128); //属性设置
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 0;
//
this.textBox1.Name = "textBox1";
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.textBox1); //添加新生成的控件textbox1
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //生成Form1类的对象,并且Application.Run提供一些必要的方法
}
}
}
///////////////////通过观察上述分析
动态生成窗体和添加控件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Form frm = new Form();
// frm.Show(); //这一句可以没有
// Create a new TextBox control and add it to the form.
TextBox textBox1 = new TextBox();
// Name the control in order to remove it later. The name must be specified
// if a control is added at run time.
textBox1.Name = "textBox1";
// Add the control to the form‘s control collection.
frm.Controls.Add(textBox1);
textBox1.Size = new Size(100, 10);
textBox1.Location = new Point(10, 10);
Application.Run(frm); //这一句务必最后写
}
}
}
标签:
原文地址:http://www.cnblogs.com/try-nocatch/p/4571492.html