zoukankan      html  css  js  c++  java
  • Winform中使用打开文件对话框和文件夹浏览对话框

    在进行winform开发的时候经常会使用打开文件对话框(OpenFileDialog)和文件夹浏览对话框(FolderBrowserDialog)。

    一、文件夹浏览对话框(FolderBrowserDialog)

    第一步、从工具箱中引入一个FolderBrowserDialog组件,当这个组件被添加到窗体上时会出现在窗体下方的空白区域,如图:

    第二步、需要一个按钮激发打开文件夹的行为,针对这个按钮btnSelectPath我们可是使用如下方法:

    private void btnSelectPath_Click(object sender, EventArgs e)
    
    {
    
        folderBrowserDialog1.Description = "请选择文件夹";
    
        folderBrowserDialog1.ShowNewFolderButton = true;
    
        folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop;
    
        DialogResult result = folderBrowserDialog1.ShowDialog();
    
        if (result == DialogResult.OK)
    
        {
    
            txtMainDir.Text = folderBrowserDialog1.SelectedPath;
    
        }
    
    }

    二、打开文件对话框(OpenFileDialog)

    第一步、同上,需要引入OpenFileDialog组件,如上图所示

    第二步、针对按钮btnSelectFile写代码:

    private void btnSelectFile_Click(object sender, EventArgs e)
    
    {
    
        openFileDialog1.Filter = "Special Files(*.xls)|*.xls|All files (*.*)|*.*";
    
        DialogResult result = openFileDialog1.ShowDialog();
    
        if (result == DialogResult.OK)
    
        {
    
            txtFile.Text = openFileDialog1.FileName;
    
        }
    
    }

    over.

  • 相关阅读:
    17 正在表达式
    16 css实现模糊背景
    15 VScode 使用相关
    14 CSS题目附答案
    13 form表单
    12 postgresql数据库备份和恢复
    11 vs2015 连接oracle 11g 数据库及相关问题
    10 windows server 2012R2 发布MVC框架网站注意事项
    9 ArcGIS Server 性能优化
    Project Euler P4 Largest palindrome product 题解
  • 原文地址:https://www.cnblogs.com/fanyong/p/3051908.html
Copyright © 2011-2022 走看看