如何在Folderbrowserdialog中过滤文件? [英] How Can I Filter Files In Folderbrowserdialog?

查看:449
本文介绍了如何在Folderbrowserdialog中过滤文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用folderBrowserDialog用imagefiles填充listview。谁知道快速的方法呢?



谢谢:)

I try to fill a listview with imagefiles by an folderBrowserDialog. Who know a fast way to do this?

Thank you :)

推荐答案

首先我是猜猜你要填写System.Windows.Forms.ListView,对吗?

其次我猜你要用文件夹中的图像文件来填充它,这个文件夹是用FolderBrowserDialog选中的,对吗?

将来你真的应该更好地指出你的问题...



不过你需要做的就是填写ListView的LargeImageList或者SmallImageList(取决于您要使用的ListView视图)与从图像文件创建的Image对象。

如下所示:

First I'm guessing you want to fill out the System.Windows.Forms.ListView, right?
Second I'm guessing you want to fill it with image files located inside a folder which is selected with FolderBrowserDialog, right?
In the future you really should specify your issues better...

Nevertheless what you need to do is fill the ListView's LargeImageList or a SmallImageList (depending on what ListView's View you are going to use) with the Image objects created from image files.
So something like the following:
if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
    this.listView1.View = View.LargeIcon;
    this.listView1.LargeImageList = new ImageList() { ImageSize = new Size(64, 64) };

    var imageFiles = from file in Directory.EnumerateFiles(this.folderBrowserDialog1.SelectedPath)
                    let extension = Path.GetExtension(file)
                    // Add more image extensions if needed ... 
                    where extension.Equals(".jpg") || extension.Equals(".png")
                    select file;

    int imageIndex = 0;
    foreach (string imageFile in imageFiles)
    {
        this.listView1.LargeImageList.Images.Add(Image.FromFile(imageFile));
        this.listView1.Items.Add(null, imageIndex++);
    }
}


我现在看到了,所以你想拖拽&将图像放到ListView控件上,这很有意义。

为此你需要启用Drag& amp;在您的应用程序中删除功能,这里是拖动和 - 的简短概述在Windows窗体中删除功能

以下是一个如何执行此操作的示例:

I see now, so you want to Drag & Drop the images onto the ListView control, well that makes sense.
For this you need to enable Drag & Drop feature in your application, here is a short overview of Drag-and-Drop Functionality in Windows Forms.
And here is an example how you can do this:
// I moved this outside.
private int imageIndex = 0;

public Form1()
{
    InitializeComponent();

    this.listView1.AllowDrop = true;

    this.listView1.DragEnter += (sender, e) =>
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            e.Effect = DragDropEffects.Copy;
    };

    this.listView1.DragDrop += (sender, e) =>
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

        // You can notice that the following lines are repetition which I also used in above sample code.
        // So you can tweak a bit your code so that you don't repeat yourself.
        var imageFiles = from file in files
                            let extension = Path.GetExtension(file)
                            where extension.Equals(".jpg") || extension.Equals(".png")
                            select file;

        foreach (string imageFile in imageFiles)
        {
            this.listView1.LargeImageList.Images.Add(Image.FromFile(imageFile));
            this.listView1.Items.Add(null, this.imageIndex++);
        }
    };
}


这篇关于如何在Folderbrowserdialog中过滤文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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