zoukankan      html  css  js  c++  java
  • 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;
    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

  • 相关阅读:
    C++顺序容器知识总结
    C++标准库vector类型的使用和操作总结
    C++迭代器的使用和操作总结
    快速入门正则表达式
    深入浅出 Create React App
    JavaScript 中有关数组对象的方法
    JavaScript 中有关时间对象的方法
    Web前端小白入门指迷
    Mac OSX 下用 Homebrew 安装 MongoDB 并配置到 WebStorm 中
    面向对象三大特性五大原则 + 低耦合高内聚
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3340691.html
Copyright © 2011-2022 走看看