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

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

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

         private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog oFD = new OpenFileDialog();
oFD.Title = "选择数据文件";//对话框标题
oFD.Filter = "所有文件(*.*)|*.*|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";//设置文件名筛选器
oFD.Multiselect = true;//是否可以多选文件
if (oFD.ShowDialog() == DialogResult.OK)
{
textBox1.Text = oFD.FileName;
}
} private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog fBD = new FolderBrowserDialog();
fBD.Description = "请选择一个文件夹";//控件上显示的说明文本
fBD.RootFolder = Environment.SpecialFolder.Desktop;//设置开始浏览的根文件夹
fBD.ShowNewFolderButton = true;//是否显示“新建文件夹”按钮
if (fBD.ShowDialog() == DialogResult.OK)
{
textBox2.Text = fBD.SelectedPath;
}
} private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog sFD = new SaveFileDialog();
sFD.Title = "保存文件对话框";//对话框标题
sFD.Filter = "所有文件(*.*)|*.*|Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";//设置文件名筛选器
sFD.InitialDirectory = @"C:/";//对话框显示的初始目录
sFD.FileName = "abc.xls";//对话框选定的文件名
if (sFD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox3.Text = sFD.FileName;
}
} private void button4_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if (cd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox4.Text = cd.Color.ToString();
//this.textBox4.Text = (cd.Color.A + ";" + cd.Color.R + ";" + cd.Color.G + ";" + cd.Color.B).ToString();
}
} private void button5_Click(object sender, EventArgs e)
{
FontDialog sd = new FontDialog();
if (sd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox5.Text = sd.Font.FontFamily.Name.ToString();
}
}
上一篇:net之工作流工程展示及代码分享(记录)


下一篇:201521044091 《Java程序设计》第5周学习总结