您的位置:首页 > 编程语言 > C#

C# winform 组件---- folderBrowserDialog与openFileDialog(转)

2013-09-26 14:03 573 查看

C# winform 组件---- folderBrowserDialog与openFileDialog

2009-06-27 13:36 2153人阅读 评论(1) 收藏 举报
winformc#buttonobject工具

vs2008 winform 的工具箱中有两个组件:folderBrowserDialog与openFileDialog.他们的作用如下:

folderBrowserDialog:打开一个浏览对话框,以便选取路经.
openFileDialog: 打开一个浏览对话框,以便选取一个文件名.

在实际操作中的应用如下:

private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
this.textBox1.Text = folderBrowserDialog1.SelectedPath;
}

private void button2_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
this.textBox2.Text = openFileDialog1.SafeFileName;
}

如果没有在窗体中拖入folderBrowserDialog与openFileDialog组件,代码也可以这样来写:
using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class SelectFolder : Form

{

public SelectFolder()

{

InitializeComponent();

}

private void btnSelectPath_Click(object sender, EventArgs e)

{

FolderBrowserDialog path =new FolderBrowserDialog();

path.ShowDialog();

this.txtPath.Text = path.SelectedPath;

}

private void btnSelectFile_Click(object sender, EventArgs e)

{

OpenFileDialog file =new OpenFileDialog();

file.ShowDialog();

this.txtFile.Text = file.SafeFileName;

}

}

}

[C#]Winform選擇目錄路徑(FolderBrowserDialog)與選擇檔案名稱(OpenFileDialog)的用法









最近寫winform的程式,剛好要用到這樣的功能

介紹如何利用FolderBrowserDialog與OpenFileDialog

來選擇目錄或檔案...

c#(winform)部分程式碼
SelectFolder.cs

01
<span style=
"display: none"
id=
"1226045165047S"
> </span>
using
System;
02
using
System.Collections.Generic;
03
using
System.ComponentModel;
04
using
System.Data;
05
using
System.Drawing;
06
using
System.Text;
07
using
System.Windows.Forms;
08
09
namespace
WindowsApplication1
10
{
11
public
partial
class
SelectFolder : Form
12
{
13
public
SelectFolder()
14
{
15
InitializeComponent();
16
}
17
18
private
void
btnSelectPath_Click(
object
sender, EventArgs e)
19
{
20
FolderBrowserDialog path =
new
FolderBrowserDialog();
21
path.ShowDialog();
22
this
.txtPath.Text = path.SelectedPath;
23
}
24
25
private
void
btnSelectFile_Click(
object
sender, EventArgs e)
26
{
27
OpenFileDialog file =
new
OpenFileDialog();
28
file.ShowDialog();
29
this
.txtFile.Text = file.SafeFileName;
30
}
31
32
}
33
}
執行結果:

1.主畫面





2.選目錄





3.選檔案





參考網址:
http://www.codeproject.com/KB/cs/csFolderBrowseDialogEx.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: