> 文章列表 > C# 初识Winform

C# 初识Winform

C# 初识Winform

1.C# 初识Winform

1.winform应用程序是一种智能客户端技术,我们可以使用winform应用程序帮助我么获得信息或者传输信息等。
2.属性
Name:后台要获得前台的控件对象,需要使用Name属性。
visible:指示一个控件是否可见
Enabled:指示一个控件是否可用
3.事件:发生一件事情
注册事件:双击控件注册的都是控件默认被选中的那个事件
触发事件:
4.在Main函数中创建的窗体对象,我们称之为这个窗体应用程序的主窗体。
也就意味着,当你将主窗体关闭后,整个应用程序都关闭了。

2.例子

当点击窗体上的按钮,弹出窗体2,点击窗体2的按钮,弹出窗体3,点击窗体3的按钮,关闭所有窗体。
窗体1代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button2_Click(object sender, EventArgs e){Form2 form = new Form2();form.ShowDialog();}private void Form1_Load(object sender, EventArgs e){Class1._a = this;}}
}

窗体2代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form2 : Form{public Form2(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Form3 form3 = new Form3();form3.ShowDialog();}}
}

窗体3代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form3 : Form{public Form3(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){Class1._a.Close();//关闭所有窗体,关闭主窗体,其他窗体也会随之关闭。}}
}

另外还需要新建一个静态类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WindowsFormsApp1
{static  class Class1{public static  Form1 _a;//创建静态字段,将主窗体,复制给字段_a;}
}