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

 

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

  2153人阅读  评论(1)  收藏  举报

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;   

    1. using System.ComponentModel;   
    2. using System.Data;   
    3. using System.Drawing;   
    4. using System.Text;   
    5. using System.Windows.Forms;   
    6.   
    7. namespace WindowsApplication1   
    8. {   
    9.     public partial class SelectFolder : Form   
    10.     {   
    11.         public SelectFolder()   
    12.         {   
    13.             InitializeComponent();   
    14.         }   
    15.   
    16.         private void btnSelectPath_Click(object sender, EventArgs e)   
    17.         {   
    18.             FolderBrowserDialog path = new FolderBrowserDialog();   
    19.             path.ShowDialog();   
    20.             this.txtPath.Text = path.SelectedPath;   
    21.         }   
    22.   
    23.         private void btnSelectFile_Click(object sender, EventArgs e)   
    24.         {   
    25.             OpenFileDialog file = new OpenFileDialog();   
    26.             file.ShowDialog();   
    27.             this.txtFile.Text = file.SafeFileName;   
    28.         }   
    29.   
    30.     }   




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

 

     
 

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

介紹如何利用FolderBrowserDialog與OpenFileDialog

來選擇目錄或檔案...

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

01<span style="display: none" id="1226045165047S"> </span>using System;
02using System.Collections.Generic;
03using System.ComponentModel;
04using System.Data;
05using System.Drawing;
06using System.Text;
07using System.Windows.Forms;
08 
09namespace 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

转载于:https://www.cnblogs.com/meimao5211/p/3340691.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值