1、ColorDialog:颜色对话框
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) { //对话框结果类型定义变量接受颜色 DialogResult dr = colorDialog1.ShowDialog(); //对话框结果确定键 if (dr == DialogResult.OK) { //改变所有文字前景色 textBox1.ForeColor = colorDialog1.Color; } }
2、FontDialog:字体对话框,设置全部字体
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) { //对话框结果类型定义变量接受字体 DialogResult dr = fontDialog1.ShowDialog(); //对话框结果确定键 if (dr == DialogResult.OK) { //改变所有文字前景色、字体 textBox1.Font = fontDialog1.Font; textBox1.ForeColor = colorDialog1.Color; } }
3、OpenFileDialog:打开文件,默认绝对路径,需要配合流操作
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Filter="文本文件|*.txt|文档|*.doc";//用户可见名1|程序可打开的文件后缀|用户可见名2|程序可打开的
//对话框打开文件类定义
DialogResult dr = openFileDialog1.ShowDialog();
//确定判断
if (dr==DialogResult.OK)
{
//流对象实例化 // 打开文件地址 应用编码类型
StreamReader sr = new StreamReader(openFileDialog1.FileName,Encoding.Default);
//文件从头读到尾
textBox1.Text = sr.ReadToEnd();
}
}
4、SaveFileDialog:保存文件
//默认保存路径 string path = ""; private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (path == null) { //对话框保存文件类 DialogResult dr = saveFileDialog1.ShowDialog(); //确定判断 if (dr == DialogResult.OK) { //写入流定义 StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); //写入文件 sw.Write(textBox1.Text); //调方法 sw.Flush(); //关流 sw.Close(); path = saveFileDialog1.FileName; } } else { //写入流定义 StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); //写入文件 sw.Write(textBox1.Text); //调方法 sw.Flush(); //关流 sw.Close(); path = saveFileDialog1.FileName; } }
5、FolderbrowserDialog:保存路径
TextBox基本方法
1、撤销
textBox1.Undo();
2、剪切
textBox1.Cut();
3、粘贴
textBox1.Paste();
4、复制
textBox1.Copy();
5、删除
textBox1.Text = textBox1.Text.Replace(textBox1.SelectedText,"");
6、全选
textBox1.SelectAll();