打开对话框和另存为对话框



 


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 SaveFileDialogApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "*.txt(文本文件)|*.txt";//设置打开文件格式 if(openFileDialog1.ShowDialog()==DialogResult.OK)//判断是否选择文件 { //实例化读取数据流 StreamReader SReader = new StreamReader(openFileDialog1.FileName,Encoding.Default); richTextBox1.Text = SReader.ReadToEnd();//显示打开文件的内容 SReader.Close(); //关闭读取数据流 } } private void button2_Click(object sender, EventArgs e) { saveFileDialog1.Filter = "*.txt(文本文件)|*.txt";//设置打开文件格式 if (saveFileDialog1.ShowDialog()==DialogResult.OK)//判断是否输入了另存为文件名 { //实例化写入数据流 StreamWriter SWrite = new StreamWriter(saveFileDialog1.FileName, true); SWrite.Write(richTextBox1.Text);//向文件中写入数据 SWrite.Close(); //关闭写入数据流 } } private void button3_Click(object sender, EventArgs e) { //设置浏览文件夹对话框的初始路径为桌面 folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop; if(folderBrowserDialog1.ShowDialog()==DialogResult.OK) { richTextBox1.Text += folderBrowserDialog1.SelectedPath; ; } } private void button4_Click(object sender, EventArgs e) { richTextBox1.Text = ""; fontDialog1.AllowVectorFonts = true;//设置用户可选择矢量字体 fontDialog1.AllowVerticalFonts = true;//设置字体对话框既显示水平字体,也可选择垂直字体 fontDialog1.FixedPitchOnly = false;//设置用户可选择不固定间距的字体 fontDialog1.MaxSize = 72;//设置可选择的最大字 fontDialog1.MinSize = 5;//设置可选择的最小字 if(fontDialog1.ShowDialog()==DialogResult.OK)//判断是否选择了字体 { if (richTextBox1.SelectedText == "")//判断是否选择了文本 richTextBox1.SelectAll();//全选文本 richTextBox1.SelectionFont = fontDialog1.Font;//设置选中的文体文本 } } private void button5_Click(object sender, EventArgs e) { richTextBox1.Text = ""; colorDialog1.AllowFullOpen = true;//设置用户自定义颜色 colorDialog1.AnyColor = true;//设置颜色对话框中显示所有颜色 colorDialog1.SolidColorOnly = false;//设置用户可以在颜色对话框中选择复杂颜色 if(colorDialog1.ShowDialog()==DialogResult.OK)//判断是否选择了颜色 { if(richTextBox1.SelectedText=="")//判断是否选择了文本 { richTextBox1.SelectAll();//全选文本 //将选定的文本颜色设置为颜色对话框中选择的颜色 richTextBox1.SelectionColor = colorDialog1.Color; } } } } }

  

posted @ 2019-05-28 13:40  玄~轩逸  阅读(323)  评论(0编辑  收藏  举报