namespace _01
{
public partial class Form1 : Form
{
Button button1;//生成Button类引用量,和应用程序有相同生命周期
public Form1()
{//下句修改主窗体标题,不指明属性(方法)所属对象,默认为Form1类的属性(方法) this.Text = "我的第一个程序";//也可写为:this.Text="我的第一个程序"; this. button1 = new Button();//生成Button类对象
this.button1.Location = new Point(25, 25);//修改button1属性location即按钮位置
this.button1.Text = "确定";//修改button1属性Text,即按钮的标题
//下句指定button1_Click函数是按钮单击事件的单击事件处理函数
this.button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);//按钮增加到窗体中,将在主窗体用户区显示出来 InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{//事件处理函数
this.button1.Text = "单击了我";//单击按钮事件执行的语句
} }
}
namespace _01
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
红色语句为什么不起作用呢?为什么初始化FORM1实倒的时候他的构造函数里面其它语句都执行了,而改form1标题这句没执行呢?谢谢~!
{
public partial class Form1 : Form
{
Button button1;//生成Button类引用量,和应用程序有相同生命周期
public Form1()
{//下句修改主窗体标题,不指明属性(方法)所属对象,默认为Form1类的属性(方法) this.Text = "我的第一个程序";//也可写为:this.Text="我的第一个程序"; this. button1 = new Button();//生成Button类对象
this.button1.Location = new Point(25, 25);//修改button1属性location即按钮位置
this.button1.Text = "确定";//修改button1属性Text,即按钮的标题
//下句指定button1_Click函数是按钮单击事件的单击事件处理函数
this.button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);//按钮增加到窗体中,将在主窗体用户区显示出来 InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{//事件处理函数
this.button1.Text = "单击了我";//单击按钮事件执行的语句
} }
}
namespace _01
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
红色语句为什么不起作用呢?为什么初始化FORM1实倒的时候他的构造函数里面其它语句都执行了,而改form1标题这句没执行呢?谢谢~!
将InitializeComponent();放在前面执行就可以了. public Form1()
{//下句修改主窗体标题,不指明属性(方法)所属对象,默认为Form1类的属性(方法)
InitializeComponent();
this.Text = "我的第一个程序";//也可写为:this.Text="我的第一个程序"; this.button1 = new Button();//生成Button类对象
this.button1.Location = new Point(25, 25);//修改button1属性location即按钮位置
this.button1.Text = "确定";//修改button1属性Text,即按钮的标题
//下句指定button1_Click函数是按钮单击事件的单击事件处理函数
this.button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);//按钮增加到窗体中,将在主窗体用户区显示出来
}
你把 InitializeComponent();
放到FORM构造函数第一句写就好了。
我试了一下这个也可注释掉,可有可无啊?
这个是初始化组件的,如果FORM里有别的控件。你不没有手动编写代码,你将看不到这些控件