将打开文件对话放入单独的类中 [英] putting open file dialogue into a separate class

查看:80
本文介绍了将打开文件对话放入单独的类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。

我有一个程序,并希望通过制作一些类并从主要调用它们来使它更整洁。大多数事情都是按钮点击 - openFileDialogue。有可能做到这一点,我会通过什么课程?



谢谢

Hi.
I have a program and would like to make it neater by making some classes and just calling them from the main. Most of the things are button clicks though - openFileDialogue. Is it possible to do that and what would I pass the class?

thanks

推荐答案

创建类,向类添加一些方法,如openFileDialogue。然后在main中创建一个新类的实例并调用你的方法。



Create the class, add a few methods to the class such as openFileDialogue. Then in main create an instance of your new class and call your methods.

namespace myNamespace
{
    class myClass
    {
      public void openDialogue() { //Do Something } 

     }
}


这是我写的用户控件移动打开文件对话框我的代码。



设置属性以访问可能有用的文件名,目录名,完整路径和各种其他文件信息。



This is a user control i wrote to move open file dialogue out of my code.

Set properties to access the Filename, dirname, full path and various other bits of file info that may be useful.

public partial class FileUploads : UserControl
{
    public string FileName { get; set; }
    public string DirectoryName { get; set; }
    public string FullPath { get; set; }
    public string Extension { get; set; }
    public string FileNameNoExt { get; set; }

    public FileUploads()
    {
        InitializeComponent();
    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
        openFileDialog1.Filter = "(*.GIF;*.JPG;*.JPEG;*.PDF;*.PNG;*.MP3;*.TIF;*.WAV;*.WMA)|*.GIF;*.JPG;*.JPEG;*.PDF;*.PNG;*.MP3;*.TIF;*.WAV;*.WMA";
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
                txtFileName.Text = openFileDialog1.FileName;
                FileName = Path.GetFileName(openFileDialog1.FileName);
                FileNameNoExt = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                DirectoryName = Path.GetDirectoryName(openFileDialog1.FileName);
                FullPath = Path.GetFullPath(openFileDialog1.FileName);
                Extension = Path.GetExtension(openFileDialog1.FileName);
        }
    }
}


这篇关于将打开文件对话放入单独的类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆