C#——winform
绑定事件(click,load)示例:
公有属性:
- name:后台获取前台的对象需要使用Name属性
- visible:控件是否可见
- enabled:控件是否可用

为form1绑定click事件,并将主窗体的对象放到静态类中
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 实现弹出窗体2
Form2 form2 = new Form2();
form2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
// 窗口加载完毕后,将当前窗体对象放入静态类中的静态字段中(资源共享)
Class1._frm1Test = this;
}
}
为form2绑定click事件
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.Show();
}
}
为form2绑定click事件(关闭所有窗体)
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 实现关闭当前窗体
//this.Close();
// 实现关闭所有窗体(关闭主窗体就会关闭所有的窗体)
Class1._frm1Test.Close();
}
}
静态类(静态类中资源共享)
public static class Class1
{
public static Form1 _frm1Test;
}
绑定事件(click,MouseEnter)示例:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/**
* 为按钮1绑定点击事件
*/
private void button1_Click(object sender, EventArgs e)
{
// 弹出弹窗
MessageBox.Show("我也爱你,么么哒!");
// 当点击弹窗的确定时,关闭主窗口
this.Close();
}
/**
* 为按钮2绑定鼠标划过事件
*/
private void button2_MouseEnter(object sender, EventArgs e)
{
// 给按钮一个新坐标
// 1.获取按钮能够活动的最大宽度,窗体宽度 - 按钮宽度
int maxWidth = this.ClientSize.Width - button2.Width;
// 2.获取按钮能够活动的最大高度,窗体高度 - 按钮高度
int maxHeight = this.ClientSize.Height - button2.Height;
// 3.生成随机坐标
Random r = new Random();
int x = r.Next(0, maxWidth + 1); // 由于不包括右边的值,所以要+1
int y = r.Next(0, maxHeight + 1);
// 4.给按钮一个随机的坐标
button2.Location = new Point(x,y);
}
private void button2_Click(object sender, EventArgs e)
{
// 弹出弹窗
MessageBox.Show("再见!!!");
// 当点击弹窗的确定时,关闭主窗口
this.Close();
}
}
绑定事件(TextChange)示例:

TextBox标签中的属性
- WordWrap默认为true,自动换行
- ScrollBars默认为none,是否显示滚动条
- Horizontal:横向
- Vertical:纵向
- Both
- passwordChar,是否隐藏输入的字符串,可以写入一个*
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// 将label1标签的文本赋值为textBox1中的文本
label1.Text = textBox1.Text;
}
}
组件timer的使用

使用时须将enable设置为true,interval属性为每个多少ms执行一次
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/**
* timer定时器,每隔1s修改一次时间。到指定的时间播放音乐
*/
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
if (DateTime.Now.Hour == 23 && DateTime.Now.Minute == 28 && DateTime.Now.Second==22)
{
// 播放音乐
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @"E:\Documents\Desktop\1.wav"; // 这个类只能播放wav文件
sp.Play();
}
}
/**
* 当窗体加载时,将时间赋值给label标签
*/
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
}
简单记事本示例:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
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 button1_Click(object sender, EventArgs e)
{
// 1.获取用户名,密码
string nameText = textName.Text;
string pwdText = textPwd.Text;
// 2.进行校验
if (nameText == "admin" && pwdText == "123")
{
MessageBox.Show("登录成功!");
// 如果登录成功,将用户名密码内些label和text隐藏
label1.Visible = false;
label2.Visible = false;
textPwd.Visible = false;
textName.Visible = false;
button1.Visible = false;
button2.Visible = false;
// 将记事本相关的显示出来
textBox1.Visible = true;
button3.Visible = true;
button4.Visible = true;
}
else
{
MessageBox.Show("用户名或密码输入错误,请重新输入");
// 将用户名密码中的文字清空
textName.Clear();
textPwd.Clear();
// 将焦点整到textName框中
textName.Focus();
}
}
private void button2_Click(object sender, EventArgs e)
{
// 将用户名密码中的文字清空
textName.Clear();
textPwd.Clear();
// 将焦点整到textName框中
textName.Focus();
}
private void button3_Click(object sender, EventArgs e)
{
// 获取button3的文字
string button3Text = button3.Text;
if (button3Text == "自动换行")
{
// 将textBox1改为自动换行
textBox1.WordWrap = true;
// 将button3的文字改为取消自动换行
button3.Text = "取消自动换行";
}
else
{
// 将textBox1改为自动换行
textBox1.WordWrap = false;
// 将button3的文字改为取消自动换行
button3.Text = "自动换行";
}
}
private void button4_Click(object sender, EventArgs e)
{
String path = @"C:\Users\x5456\Desktop\new.txt";
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
string str = textBox1.Text;
byte[] bytes = Encoding.UTF8.GetBytes(str);
stream.Write(bytes, 0, bytes.Length);
}
MessageBox.Show("保存成功!");
}
}
}
单选框的使用
一个from中只能选中一个单选框,如果要分级的话,要使用groupbox容器进行分割

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 WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 判断单选框是否被选中
if (radioStu.Checked || radioTea.Checked)
{
string name = textBox1.Text;
string pwd = textBox2.Text;
if (radioStu.Checked)
{
if (name == "student" && pwd == "123")
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("登录失败!");
}
}
else
{
if (name == "teacher" && pwd == "123")
{
MessageBox.Show("登录成功!");
}
else
{
MessageBox.Show("登录失败!");
}
}
}
else
{
MessageBox.Show("请先选择登录身份");
}
}
}
}
MDI窗体设计

使用步骤
- 1)新建几个form窗体
- 2)找一个窗体,将它的isMdiContainer属性设置为True
- 3)拖入插件MenuStrip
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 WindowsFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 显示子窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 创建Form窗体的对象
Form2 form2 = new Form2();
// 告诉它的父窗体是当前窗体
form2.MdiParent = this;
form2.Show();
Form3 form3 = new Form3();
form3.MdiParent = this;
form3.Show();
}
private void 纵向排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
private void 横向排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
}
}
pictureBox控件的使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 获取目录下的所有文件路径
private string[] strings = Directory.GetFiles(@"F:\快盘\宝岩\没有这幅画、\Lovers");
private int i = 0;
/// <summary>
/// form窗体的load事件,当窗体加载完毕,为图片控件加载一个图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
// 设置展示格式
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
// 将第一张图片加载到图片控件中
pictureBox1.Image = Image.FromFile(strings[0]);
}
private void button2_Click(object sender, EventArgs e)
{
if (i >= strings.Length-1)
{
i = -1;
}
pictureBox1.Image = Image.FromFile(strings[++i]);
}
private void button1_Click(object sender, EventArgs e)
{
if (i<=0)
{
i = strings.Length;
}
pictureBox1.Image = Image.FromFile(strings[--i]);
}
}
}
WebBrowser控件的使用

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 WindowsFormsApp5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string text = textBox1.Text;
webBrowser1.Url = new Uri("http://"+text);
}
}
}
ComboBox下拉框控件

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 WindowsFormsApp6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/**
* 当form窗体加载完毕时,将年加载
*/
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1996; i < 2099; i++)
{
cboYear.Items.Add(i + "年");
}
}
/**
* 当年的select框改变时,加载月
*/
private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
{
// 先将月份中的内容清空
cboMon.Items.Clear();
for (int i=1; i <= 12; i++)
{
cboMon.Items.Add(i+"月");
}
}
/**
* 当月改变时,加载日中的内容
*/
private void cboMon_SelectedIndexChanged(object sender, EventArgs e)
{
// 清空日中的内容
cboDay.Items.Clear();
// 获取年月的信息,并将其转换成int型
int year = Convert.ToInt32(cboYear.SelectedItem.ToString().Replace("年",""));
int month = Convert.ToInt32(cboMon.SelectedItem.ToString().Replace("月",""));
int day;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: day = 31;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
day = 29;
}
else
{
day = 28;
}
break;
default: day = 30;
break;
}
for (int i = 1; i <= day; i++)
{
cboDay.Items.Add(i + "日");
}
}
}
}
ListBox控件的使用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// 1.获取出文件夹下的所有文件
string[] files = Directory.GetFiles(@"F:\快盘\宝岩\没有这幅画、\Lovers","*.jpg");
/**
* 当窗体加载完毕时,加载ListBox中的内容
*/
private void Form1_Load(object sender, EventArgs e)
{
// 2.循环加入ListBox中
foreach (string file in files)
{
// 获取文件名
listBox1.Items.Add(Path.GetFileName(file));
}
}
/**
* 双击时,将图片加载到pictureBox中
*/
private void listBox1_DoubleClick(object sender, EventArgs e)
{
// listBox1.SelectedIndex获取选中的索引值
pictureBox1.Image = Image.FromFile(files[listBox1.SelectedIndex]);
}
}
}
打开文件对话框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 初始化文件对话框
OpenFileDialog ofd = new OpenFileDialog();
// 设置对话框的标题
ofd.Title = "请选择要打开的文本文件";
// 设置对话框可以多选
ofd.Multiselect = true;
// 设置对话框的初始目录
ofd.InitialDirectory = @"C:\Users\x5456\Desktop";
// 设置对话框要求的文件类型
ofd.Filter = "文本文件|*.txt|媒体文件|*.wmv|图片文件|*.jpg|所有文件|*.*";
// 展示对话框
ofd.ShowDialog();
// 获取选中的文件路径
string path = ofd.FileName;
// 如果没选择路径,则结束
if (path=="")
{
return;
}
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
// 2.建立一个字节数组,用来一点一点取数据
byte[] bytes = new byte[1024 * 1024 * 5];
// 3.读取5M的数据,返回值为本次读取文件的字节大小
int r = stream.Read(bytes, 0, bytes.Length);
// 4.字节转为字符串
string s = Encoding.UTF8.GetString(bytes, 0, r);
// 5.将返回字符串赋值给textBox
textBox1.Text = s;
}
}
}
}
保存文件对话框

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 创建保存文件对话框对象
SaveFileDialog sfd = new SaveFileDialog();
// 设置标题
sfd.Title = "请选择要保存的路径。";
// 设置初始保存路径
sfd.InitialDirectory = @"C:\Users\x5456\Desktop";
// 设置对话框要求的文件类型
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
// 显示对话框
sfd.ShowDialog();
// 获取保存的路径
string path = sfd.FileName;
// 如果没选路径,直接结束方法
if (path=="")
{
return;
}
// 保存文件
using (FileStream stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
string str = textBox1.Text;
byte[] bytes = Encoding.UTF8.GetBytes(str);
stream.Write(bytes, 0, bytes.Length);
}
}
}
}
字体颜色对话框

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 WindowsFormsApp10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 初始化字体对话框对象
FontDialog fd = new FontDialog();
// 将对话框显示出来
fd.ShowDialog();
// 将textBox1的字体改为选择字体
textBox1.Font = fd.Font;
}
private void button2_Click(object sender, EventArgs e)
{
// 初始化颜色对话框对象
ColorDialog cd = new ColorDialog();
// 将对话框显示出来
cd.ShowDialog();
// 将textBox1的字体颜色改为选择颜色
textBox1.ForeColor = cd.Color;
}
}
}
panal容器的使用

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 WindowsFormsApp11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
}
}
浙公网安备 33010602011771号