.Net学习 第3季01 winform应用程序 Form Button TextBox Label Timer DateTime SoundPlayer

.Net 视频学习第3季 winform应用程序 基础

 

每个控件/窗口都是一个类,每拖一个控件相当于new一个该控件类的对象。

 

事件:本质是一个方法

事件的两个参数:控件对象,触发条件

 

Application.Run(new Form1());

这句代码首先创建了一个Form1类的对象,然后运行。Form1类是一个窗口类。追踪其构造方法:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }

Form1类继承于From类,其构造方法里边执行了一个名为InitializeComponent()的方法。顾名思义,这个方法是初始化这个窗口对象中的控件。注意,窗口本身也有属性,所以就算窗口对象中没有控件,方法里边也会有一些代码对窗口对象的属性进行赋值:

private void InitializeComponent()
        {
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(903, 292);this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

this就是指代窗口对象本身。

每当拖动一个控件到窗口中时,相当于这个窗口对象(Form1类)中添加了一个控件类的对象。例如拖动一个button控件到窗口中,Form1类中就要添加一个成员(字段),这个成员是一个Button类的对象。因此在初始化窗口的时候,除了对窗口本身的属性进行赋值外,还需要new一个Button类的对象,添加到Form1类中,并且对这个Button类的对象进行属性赋值。

private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(362, 78);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(903, 292);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

首先代码

this.button1 = new System.Windows.Forms.Button();

创建了一个Button类的对象,并且这个对象是窗口(this)的一个成员。

// 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(362, 78);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;

紧接着这段代码对这个窗口成员(Button类对象)进行属性赋值。当然在原来对窗口进行属性赋值的部分还多了一句

this.Controls.Add(this.button1);

 

控件常见属性:

Name:后台获得前台控件

Text:控件上显示的内容

Anchor:与窗口各边是否锁定距离(窗口缩放时看效果)

BackColor:控件背景颜色

BackgroundImage:控件背景图像

BackgroundImageLayout:控件背景图像布局(Zoom居中,Strech填充)

Cursor:光标移动到控件区域时显示状态(WaitCursor等待)

Visible:控件是否可见

Enable:控件是否可用

另外补充一点,窗体的宽和高不一定是定值(用户可以缩放),如果想要获得当前的窗体宽高,需要用

int x = this.ClientSize.Width;
int y = this.ClientSzie.Height;

 

当我们在控件的属性窗口中修改属性时,会发现在代码有相应的代码。

 

ContextMenuStrip:右键菜单

要使用控件的右键菜单属性,首先要先定义好这个控件的右键菜单,例如创建一个ContextMenuStrip1的右键菜单,然后再控件的ContextMenuStrip项中选择ContextMenuStrip1(绑定右键菜单ContextMenuStrip1)。返回来看看代码,在Form1类的构造方法所调用的InitializeComponent()中,会先创建一个ContextMenuStrip类的对象作为Form1的成员ContextMenuStrip1,(并且菜单中每一个项都会生成一个ToolStripMenuItem类的对象,这些对象是ContextMenuStrip1的成员),最后控件Button类对象Button1的成员ContextMenuStrip绑定为ContextMenuStrip1。

this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.button1 = new System.Windows.Forms.Button();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.copyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.pasteToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.cutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // button1
            // 
this.button1.ContextMenuStrip = this.contextMenuStrip1;
            this.button1.Location = new System.Drawing.Point(70, 43);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(506, 324);
            this.button1.TabIndex = 0;
            this.button1.Text = "SRF";
            this.button1.TextAlign = System.Drawing.ContentAlignment.TopLeft;
            this.button1.UseVisualStyleBackColor = true;
            // 

部分相关的代码如上,可以看到,右键菜单中我们定义了四个小项,每个小项都是一个ToolStripMenuItem的对象。

 

控件的事件,本质上是一个方法。事件的参数包括object类对象(所有类都是object类的子类)。注意方法的名称中的button1之类的字样,并不意味着调用它的就是button1,每个控件都可以触发这个方法,需要的是将控件的触发动作和触发方法绑定起来。

 

假如要实现点击button1之后创建新窗口Form2和显示新窗口Form2

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            frm2.Show();
        }
    }

如果要关闭控件所在的窗口:

this.close();

注意,虽然事件是由控件来触发的(控件作为方法的输入,Object sender),但是方法本身是属于窗口的,所以方法内的this指代的是窗口,不是触发它的控件!

 

如果要关闭非控件所在的窗口,常用的技巧是,创建一个public静态类,类中有public静态成员用以存储加载了的窗口,每一个窗口类中都可以通过这个public静态类访问到其它窗口,从而调用它们的close方法。

例如,创建了Form1,Form2和Form3,其中Form1是主窗体,点击Form3中的button时关闭Form2。那么我们需要实现上面的静态类,并且在窗体加载(LOAD)的同时调用方法,将加载的窗体存放到静态类中的静态成员内。

静态类:

public static class test
    {
        public static Form1 _frm1;
        public static Form2 _frm2;
        public static Form3 _frm3;
    }

加载时存储窗体Form2对象的实现:

private void Form2_Load(object sender, EventArgs e)
        {
            test._frm2 = this;
        }

窗体Form3中button的单击事件实现:

private void button1_Click(object sender, EventArgs e)
        {
            test._frm2.Close();
        }

 

如果希望单击窗体Form3中button时关闭所有窗体,则需要关闭主窗体Form1,代码类似。

 

如果希望单击button时弹出一个message box也可以直接用以下代码:

private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hahaha!");
        }

 

如果想光标进入控件可见区域时触发事件,可以在事件触发中选择条件MouseEnter。

 

文本框TextBox类,输入文本

WordWrap:是否自动换行

ScrollBars:滚动条设置

PasswordChar:输入字符将全部以指定字符显示

 

Label类,显示文本

 

组件Timer类

Enable:默认false

Interval:间隔多少时间触发一次时间,毫秒为单位

---跑马灯效果:每个Xms触发Timer的默认事件Tick,将Label的文本的首字符放到最后

 private void timer1_Tick(object sender, EventArgs e)
        {
            lalText.Text = lalText.Text.Substring(1) + lalText.Text.Substring(0, 1);
        }

 

DateTime类,成员Now,获取当前日期和时间。如果要显示在Label中,先调用ToString()方法。在窗体加载时以及Timer每隔1000ms时触发事件

private void Form1_Load(object sender, EventArgs e)
        {
            lblTimeShow.Text = DateTime.Now.ToString();
        }

 

private void timer2_Tick(object sender, EventArgs e)
        {
            lblTimeShow.Text = DateTime.Now.ToString();
        }

 

---闹钟效果:判断当前时间是否为指定时间,若是,执行音乐播放,需要用到SoundPlayer类(命名空间:System.Media)

private void timer2_Tick(object sender, EventArgs e)
        {
            lblTimeShow.Text = DateTime.Now.ToString();
            if (DateTime.Now.Hour == 16 && DateTime.Now.Minute == 46 && DateTime.Now.Second == 30)
            {
                SoundPlayer sp = new SoundPlayer();
                sp.SoundLocation = @"C:\...\XX.wav";
                sp.Play();
            }
        }

注意SoundPlayer只能播放wav文件。

 

Form类的默认事件是加载,Button类的默认事件是单击,TextBox类的默认事件是文本改变

posted @ 2015-01-07 17:45  Cyrus Ho  阅读(227)  评论(0编辑  收藏  举报