首页 > 代码库 > Winform 五种常用对话框控件的简单使用

Winform 五种常用对话框控件的简单使用

OpenFileDialog(打开文件对话框)
FolderBrowserDialog(浏览文件夹对话框)
SaveFileDialog(保存文件对话框)
ColorDialog(颜色对话框)
FontDialog(字体对话框)

技术分享

 

技术分享
 1         private void button1_Click(object sender, EventArgs e) 2         { 3             OpenFileDialog oFD = new OpenFileDialog(); 4             oFD.Title = "选择数据文件";//对话框标题 5             oFD.Filter = "所有文件(*.*)|*.*|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";//设置文件名筛选器 6             oFD.Multiselect = true;//是否可以多选文件 7             if (oFD.ShowDialog() == DialogResult.OK) 8             { 9                 textBox1.Text = oFD.FileName;10             }11         }12 13         private void button2_Click(object sender, EventArgs e)14         {15             FolderBrowserDialog fBD = new FolderBrowserDialog();16             fBD.Description = "请选择一个文件夹";//控件上显示的说明文本17             fBD.RootFolder = Environment.SpecialFolder.Desktop;//设置开始浏览的根文件夹18             fBD.ShowNewFolderButton = true;//是否显示“新建文件夹”按钮19             if (fBD.ShowDialog() == DialogResult.OK)20             {21                 textBox2.Text = fBD.SelectedPath;22             }23         }24 25         private void button3_Click(object sender, EventArgs e)26         {27             SaveFileDialog sFD = new SaveFileDialog();28             sFD.Title = "保存文件对话框";//对话框标题29             sFD.Filter = "所有文件(*.*)|*.*|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";//设置文件名筛选器30             sFD.InitialDirectory = @"C:/";//对话框显示的初始目录31             sFD.FileName = "abc.xls";//对话框选定的文件名32             if (sFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)33             {34                 this.textBox3.Text = sFD.FileName;35             }36         }37 38         private void button4_Click(object sender, EventArgs e)39         {40             ColorDialog cd = new ColorDialog();41             if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)42             {43                 this.textBox4.Text = cd.Color.ToString();44                 //this.textBox4.Text = (cd.Color.A + ";" + cd.Color.R + ";" + cd.Color.G + ";" + cd.Color.B).ToString();45             }46         }47 48         private void button5_Click(object sender, EventArgs e)49         {50             FontDialog sd = new FontDialog();51             if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)52             {53                 this.textBox5.Text = sd.Font.FontFamily.Name.ToString();54             }55         }
View Code

 

<style></style>
 

Winform 五种常用对话框控件的简单使用