//当点击打开的时候 private string files; //建一个成员变量,用来存储打开的路径 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) { //openFileDialog1.Filter = "文本文件|*txt|视频文件|*avi;*jpg"; //filter建立一个筛选器 //openFileDialog1.ShowDialog(); DialogResult isok = openFileDialog1.ShowDialog(); if (isok == DialogResult.OK) //判断是否点击的打开按钮 { string filename = openFileDialog1.FileName;//获取文件路径,文件路径存在FileName里 //使用流进行文件读取 StreamReader sr = new StreamReader(filename); //构建一个streamreader类的对象sr ,文件路径作为参数 textBox1.Text = sr.ReadToEnd(); //readtoend方法,把文件完全读出 sr.Close(); //流用完一定关闭 files = filename; } //当点击另存为按钮的时候 private void 保存sToolStripMenuItem_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "文本文件|*.txt"; //要保存的文件格式。Filter文件筛选器 DialogResult isok = saveFileDialog1.ShowDialog(); //是否点击保存按钮 if (isok==DialogResult .OK) { string filename = saveFileDialog1.FileName; StreamWriter sw = new StreamWriter(filename); sw.Write(textBox1.Text); sw.Close(); //流用完一定要关闭 } MessageBox.Show(saveFileDialog1 .FileName ); } //当点击保存按钮的时候 private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e) { if (files == null) { DialogResult baocun = saveFileDialog1.ShowDialog(); if (baocun == DialogResult.OK) { string filename = saveFileDialog1.FileName; StreamWriter sw = new StreamWriter(filename); sw.Write(textBox1.Text); sw.Close(); } } else { StreamWriter sw = new StreamWriter(files); sw.Write(textBox1.Text); sw.Close(); } } //当点击颜色的时候 private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e) { //colorDialog1.ShowDialog();//显示颜色对话框,要显示对话框都有showdialog这个共同属性。。 colorDialog1.ShowDialog()会有一个返回值,用dialogresult这个枚举类型来接受 DialogResult dr = colorDialog1.ShowDialog(); if (dr==DialogResult .OK) //判断是否点击的确定按钮 { this.BackColor = colorDialog1.Color; //如果仅仅写这一句代码,在选完颜色之后点取消,会默认系统里的一个黑色 } } //点击文件浏览器获取路径 private void 文件浏览器ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dr = folderBrowserDialog1.ShowDialog(); //接受打卡文件路径时的返回值 if (dr==DialogResult .OK) { MessageBox.Show(folderBrowserDialog1.SelectedPath ); //selectedpath获取路径的属性 } } //当点击新建的时候 private void 新建NToolStripMenuItem_Click(object sender, EventArgs e) { if (textBox1 .Text .Length>0) { saveFileDialog1.ShowDialog(); DialogResult dr = new DialogResult(); if (dr == DialogResult.OK) { string filename = saveFileDialog1.FileName; StreamWriter sw = new StreamWriter(filename); sw.Write(textBox1.Text); sw.Close(); } else { textBox1.Text = ""; } } }
时间: 2024-10-08 18:38:47