各种对话框

  1. //选择文件对话框
  2. //OpenFileDialog f = new OpenFileDialog();
  3. //另存为对话框
  4. //SaveFileDialog f = new SaveFileDialog();
  5. //字体对话框
  6. //FontDialog f = new FontDialog();
  7. //颜色对话框
  8. ColorDialog f = new ColorDialog();
  9. f.ShowDialog();

选择文件对话框

  1. //点击弹出对话框
  2. OpenFileDialog ofd = new OpenFileDialog();
  3. //设置对话框的标题
  4. ofd.Title = "请选择要打开的文本文件哟亲 O(∩_∩)O~";
  5. //设置对话框可以多选
  6. ofd.Multiselect = true;
  7. //设置对话框的初始目录
  8. ofd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
  9. //设置对话框的文件类型
  10. ofd.Filter = "文本文件|*.txt|媒体文件|*.wmv|图片文件|*.jpg|所有文件|*.*";
  11. //展示对话框
  12. ofd.ShowDialog();
  13. //获得在打开对话框中选中文件的路径
  14. string path = ofd.FileName;
  15. if (path == "")
  16. {
  17. return;
  18. }
  19. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
  20. {
  21. byte[] buffer = new byte[1024 * 1024 * 5];
  22. //实际读取到的字节数
  23. int r = fsRead.Read(buffer, 0, buffer.Length);
  24. textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
  25. }

保存对话框

  1. SaveFileDialog sfd = new SaveFileDialog();
  2. sfd.Title = "请选择要保存的路径";
  3. sfd.InitialDirectory = @"C:\Users\SpringRain\Desktop";
  4. sfd.Filter = "文本文件|*.txt|所有文件|*.*";
  5. sfd.ShowDialog();
  6. //获得保存文件的路径
  7. string path = sfd.FileName;
  8. if (path == "")
  9. {
  10. return;
  11. }
  12. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
  13. {
  14. byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
  15. fsWrite.Write(buffer, 0, buffer.Length);
  16. }
  17. MessageBox.Show("保存成功");

字体对话框

  1. FontDialog fd = new FontDialog();
  2. fd.ShowDialog();
  3. textBox1.Font = fd.Font;

颜色对话框

  1. ColorDialog cd = new ColorDialog();
  2. cd.ShowDialog();
  3. textBox1.ForeColor = cd.Color;