一、控件
1、WebBrowser
- 浏览器控件
1>属性
- Url:导航的网址(在属性栏输入时会自动补全http://)
2、ComboBox
- 下拉框控件:建议以cbo…格式命名
1>属性
- Items:下拉框中的显示内容(可在属性栏中的编辑项、Items以及点击控件上的小三角进行输入)
- DropDownStyle:控制下拉框的外观格式
2>事件
- SelectedIndexChanged:当前下拉框中的值被选中的时候发生事件
3、ListBox
- 集合:类似于音乐播放的菜单栏,属性与事件与ComboBox类似
1>属性
- Items:下拉框中的显示内容(可在属性栏中的编辑项、Items以及点击控件上的小三角进行输入)
2>事件
- DoubleClick:双击时触发事件
4、Panel
- 容器:与GroupBox类似
5、DataGridView
- 用于显示表格数据
- 更改中文表名并绑定相关数据列的操作
二、对话框
- OpenFileDialog:打开文件对话框
- SaveFileDialog:保存文件对话框
- FontDialog:字体对话框
- ColorDialog:颜色对话框
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;
using System.IO;
namespace 文本框
{
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\Administrator\Desktop";
ofd.Filter = "文本文件|*.txt|媒体文件|*.wav|图片文件|*.jpg|所有文件|*.*";
ofd.ShowDialog();
string path = ofd.FileName;
if (path == "")
return;
using (FileStream fsRead = new FileStream(path,FileMode.Open,FileAccess.Read))
{
byte[] buffer =new byte[1024 * 1024 * 5];
int r = fsRead.Read(buffer, 0, buffer.Length);
string str = Encoding.Default.GetString(buffer, 0, r);
textBox1.WordWrap = true;
textBox1.Text = str;
}
}
}
}
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;
using System.IO;
namespace 保存文件对话框
{
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\Administrator\Desktop";
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
sfd.ShowDialog();
string path = sfd.FileName;
if (path == "")
return;
using(FileStream fsWrite = new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
fsWrite.Write(buffer, 0, buffer.Length);
}
MessageBox.Show("保存成功");
}
}
}
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 字体和颜色对话框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bntFont_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
private void bntColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.ForeColor = cd.Color;
}
}
}
三、案例代码
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 浏览器控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
string text = textBox1.Text;
webBrowser1.Url = new Uri("http://"+text);
}
}
}
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 日期选择器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int year = DateTime.Now.Year;
for (int i = year; i >= 1949; i--)
{
cboYear.Items.Add(i +