6.30 winform 对话框控件
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Windows.Forms; 10 11 namespace _6._30_上午_对话框控件 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void Form1_Load(object sender, EventArgs e) 21 { 22 23 } 24 //设置颜色 25 private void toolStripMenuItem2_Click(object sender, EventArgs e) 26 { 27 DialogResult dr = colorDialog1.ShowDialog(); 28 if (dr == DialogResult.OK) 29 { 30 textBox1.ForeColor = colorDialog1.Color; 31 } 32 33 } 34 //选择文件夹 35 private void 选择文件夹ToolStripMenuItem_Click(object sender, EventArgs e) 36 { 37 folderBrowserDialog1.ShowDialog();//选择文件夹 38 textBox1.Text=folderBrowserDialog1.SelectedPath;//显示路径 39 } 40 41 private void 设置字体ToolStripMenuItem_Click(object sender, EventArgs e) 42 { 43 fontDialog1.ShowColor = true;//可选择颜色 44 fontDialog1.ShowDialog();//显示设置字体框 45 textBox1.Font = fontDialog1.Font;//可变换字体 46 textBox1.ForeColor = fontDialog1.Color; 47 } 48 49 private void fontDialog1_Apply(object sender, EventArgs e) 50 { 51 52 } 53 //打开文件,并规定允许打开的类型 54 private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e) 55 { 56 openFileDialog1.Filter = "文本文件|*.txt|*.*"; 57 DialogResult dr = openFileDialog1.ShowDialog(); 58 if (dr == DialogResult.OK) 59 { 60 label1.Text = openFileDialog1.FileName; 61 StreamReader sr = new StreamReader(openFileDialog1.FileName);//引用生成流文件 62 textBox1.Text = sr.ReadToEnd();//打开文件打开后是乱码 63 sr.Close(); 64 65 66 } 67 } 68 69 //保存,并分情况判断 70 string path = ""; 71 72 private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) 73 { 74 if (path == "")//判断是否保存过,未保存选择路径保存 75 { 76 saveFileDialog1.FileName = "新建文本文件.txt"; 77 saveFileDialog1.ShowDialog(); 78 label1.Text = saveFileDialog1.FileName; 79 } 80 StreamWriter sw = new StreamWriter(path);//已经保存还是保存在原路径 81 sw.Write(textBox1.Text); 82 sw.Close(); 83 84 } 85 } 86 }