windows窗体控件要点摘录

     刚开始学windows控件的时候感觉很痛苦,像VB但又和VB有很大不同,似懂非懂,让人摸不着头脑。每当在这个时候,我都能看见自己内心的焦虑,一方面我要照理好自己的情绪,另一方面也要慢慢学习,虽然期间很难,但是坚持下去以后,终究会雨过天晴的。

     我又努力听视频课,虽然不怎么懂,不止步的继续学习后面的类和对象,我相信每一门知识前后总能衔接,边学后面的边看前面的,慢慢融会贯通,效果更好,当然在课下看C#入门经典(第四版)也让我了解了很多,忍不住要记录下来。

      比如往窗体上添加一个按钮。

       using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace e2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;//定义Button类引用变量
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法- 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

 

      {
this.button1 = new System.Windows.Forms.Button();//生成对象
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(96, 56);//修改属性
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 32);
this.button1.TabIndex = 0;
this.button1.Text = "确定";
this.button1.Click += new System.EventHandler(this.button1_Click);//增加事件
//
// Form1

     //
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
this.Name = "Form1";
this.Text = "Form1";
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click  (objectsender,System.EventArgs e)
{//事件处理函数
}
}
}

请注意按钮放到窗体后,集成环境自动增加的语句。分析这些增加的语句,可知在窗体中增加Button类对象的步骤:首先定义Button类变量button1,这是Form1类的一个字段,由于主窗体关闭,程序也就结束了,因此定义在主窗体Form1中的变量的生命周期和程序的生
命周期是相同的,从这个意义上说,这样的变量是全局变量。因此变量button1和主窗体Form1有相同的生命周期。第二步在构造函数中用new生成Button类对象,第三步在构造函数中修
改button1的属性,第四步增加button1的事件函数,函数button1_Click()是事件处理函数,语句this.button1.Click += new System.EventHandler(this.button1_Click)把按钮
Button1的事件Click和事件处理函数button1_Click()联系到一起。程序员应在事件处理函数button1_Click()中增加具体的事件处理语句。这些步骤对于增加任何控件都是相同的。应熟悉以上操作步骤,学会在窗体中增加控件,修改控件属性,增加事件函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值