对话框的应用

1、ColorDialog颜色对话框
  1.1实例介绍
    改变窗体背景颜色
  1.2 开发过程
    创建一个winform应用程序,在窗体中添加一按钮控件和一ColorDialog控件
  1.3代码实现

1  private void button1_Click(object sender, EventArgs e)
2         {
3             if(colorDialog1.ShowDialog()==DialogResult.OK)
4             {
5                 this.BackColor = colorDialog1.Color;
6             }
7         }
View Code

2、FolderBrowserDialog 文件夹浏览对话框
  2.1开发过程
   创建一winform应用程序,在一窗体上添加一按钮控件、一FolderBrowserDialog控件
  2.2代码实现

1  private void button1_Click(object sender, EventArgs e)
2         {
3             if(folderBrowserDialog1.ShowDialog()==DialogResult.OK)
4             {
5                 //把选中文件夹的路径赋值给窗体标题
6                 this.Text = folderBrowserDialog1.SelectedPath;
7             }
8         }
View Code

3、FontDialog 对话框
  3.1 实例介绍
    使用FontDialog对话框调用Windows的字体对话框,改变字体的字体、颜色
  3.2 开发过程
    创建一个winform应用程序,向窗体上添加一个按钮控件、label控件、FontDialog 对话框控件,设置label1.text="改变字体";
  3.3 代码实现

1  private void button1_Click(object sender, EventArgs e)
2         {
3             if(fontDialog1.ShowDialog()==DialogResult.OK)
4             {
5                label1.font=fontDialog1.font;
6                label1.ForeColor=fontDialog1.Color;
7             }
8         }
View Code

4、OpenFileDialog对话框
  4.1界面设计
    创建一个winform应用程序,添加一个窗体、在窗体上添加一个按钮、一个label、textBox;
  4.2 功能描述
   运行程序,单击“打开文件”按钮,在弹出的“打开”对话框选择文件,文件路径显示在label控件中,文件内容显示在textbox文件中
  4.3代码实现

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 
10 namespace Kaifafanli
11 {
12     public partial class Form9 : Form
13     {
14         public Form9()
15         {
16             InitializeComponent();
17         }
18 
19         private void button1_Click(object sender, EventArgs e)
20         {
21 
22             openFileDialog1.InitialDirectory = "c:\";//文件对话框显示的初始目录
23             openFileDialog1.Filter = "文本文件(*.txt)|*.txt|All Files(*.*)|*.*";//设置过滤条件
24             openFileDialog1.FilterIndex = 2;//在对话框的文件筛选器中默认选择文件类型
25             openFileDialog1.RestoreDirectory = true;//控制对话框在关闭之前是否恢复当前目录
26             openFileDialog1.ShowHelp = true;//是否启用帮助按钮,单击帮助按钮可以执行openFileDialog1_HelpRequest方法
27             openFileDialog1.Title = "文件查找";
28             openFileDialog1.CheckFileExists = true;
29             if(openFileDialog1.ShowDialog()==DialogResult.OK)
30             {
31                 label1.Text = openFileDialog1.FileName;//文件名
32                 System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
33                 textBox1.Text = sr.ReadToEnd();
34                 sr.Close();
35             }
36         }
37 
38         private void openFileDialog1_HelpRequest(object sender, EventArgs e)
39         {
40             MessageBox.Show("你单击了帮助按钮!");
41         }
42     }
43 }
View Code

5、SaveFileDialog
  5.1开发过程
    创建一个winform应用程序,在窗体上添加一个按钮“保存文件”,添加一个textbox文本框
  5.2 实现功能
    运行程序,在文本框中输入要保存的文字,单击“保存文件”按钮,实现文件保存
  5.3 代码实现

 1 private void button1_Click(object sender, EventArgs e)
 2         {
 3             StreamWriter myStream;
 4 
 5             saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
 6 
 7             saveFileDialog1.FilterIndex = 2;
 8 
 9             saveFileDialog1.RestoreDirectory = true;
10 
11             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
12             {
13 
14                 myStream = new StreamWriter(saveFileDialog1.FileName);
15 
16                 myStream.Write(textBox1.Text); //写入
17 
18                 myStream.Close();//关闭流
19 
20             }
21         }
View Code
原文地址:https://www.cnblogs.com/net064/p/5669846.html