【2017-5-2】Winform 对话框控件
对话框控件:
ColorDialog - 颜色选择
ShowDialog()
DialogResult 类型
对象.Color
FontDialog - 字体选择
ShowDialog()
DialogResult
对象.Font
对象.Color
--------------------
OpenFileDialog - 文件打开对话框
ShowDialog()
对象.FileName
有文件路径,如何打开此文件?
使用 流
using System.IO
SaveFileDialog - 文件保存位置对话框
folderBrowserDialog - 选择文件对话框,只能看到路径,看不到文件
写在构造函数下面
private void 撤消UToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Undo(); } private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Cut(); } private void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Paste(); } private void 复制CToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Copy(); } private void 全选AToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.SelectAll(); } private void 时间ToolStripMenuItem_Click(object sender, EventArgs e) { textBox1.Text += DateTime.Now.ToString("HH:mm yyyy-MM-dd"); } private void 查找ToolStripMenuItem_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this); f2.Owner = this; f2.Show(); } private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e) { if (textBox1.WordWrap) { textBox1.WordWrap = false; textBox1.ScrollBars = ScrollBars.Both; } else { textBox1.WordWrap = true; textBox1.ScrollBars = ScrollBars.Vertical; } } private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult drr = colorDialog1.ShowDialog(); if (drr == DialogResult.OK) { textBox1.ForeColor = colorDialog1.Color; } } private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dr = fontDialog1.ShowDialog(); if (dr == DialogResult.OK) { textBox1.Font = fontDialog1.Font; textBox1.ForeColor = fontDialog1.Color; } } private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) { openFileDialog1.Filter = "文本文件|*.txt|文档|*.doc|所有文件|*.*"; DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.Default); textBox1.Text = sr.ReadToEnd(); } } string savePath = ""; private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (savePath == "") { saveFileDialog1.FileName = "*.txt"; saveFileDialog1.Filter = "文本文件|*.txt"; DialogResult dr = saveFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(textBox1.Text); sw.Flush(); sw.Close(); savePath = saveFileDialog1.FileName; } } else { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName); sw.Write(textBox1.Text); sw.Flush(); sw.Close(); savePath = saveFileDialog1.FileName; } }