简体   繁体   中英

Don't display filter extensions in OpenFileDialog

Why don't filter extensions display in OpenFileDialog ? I tested this but the dialog does not show the DAT extension. If I get the DAT extension in the first of the list extension, then OpenFileDialog filters in the dialog. I am using C# Application- FrameWork 3.5 - on Win XP.

Here is my code:

OpenFileDialog openFileDialog = new OpenFileDialog();

string VideoFormat = "Video files |*.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                          " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm; *.dat; ";


openFileDialog.Filter = VideoFormat;
openFileDialog.ShowDialog();

but this code filtered DAT extension in dialog:

OpenFileDialog openFileDialog = new OpenFileDialog();

            string VideoFormat = "Video files | *.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                      " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm; ";


openFileDialog.Filter = VideoFormat;
openFileDialog.ShowDialog();

This is the correct formatting for the Filter property of your OpenFileDialog object:

(*.jpg)|*.jpg|Image Files (*.png)|*.png|Image Files (*.jpeg)|*.jpeg

Try this:

        OpenFileDialog openFileDialog = new OpenFileDialog();
        string formats = "*.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                  " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm";

        string[] exts = formats.Split(';');
        string filter = string.Empty;
        foreach (string ext in exts)
        {

                filter += "Video Files (" + ext.Replace("*", "").Trim() + ")|" + ext + "|";
        }

        openFileDialog.Filter = filter.Remove(filter.Length-1,1);
        openFileDialog.ShowDialog();

UPDATE

This one shows just one option, but will let you select all of the video types:

        OpenFileDialog openFileDialog = new OpenFileDialog();
        string formats = "All Videos Files |*.dat; *.wmv; *.3g2; *.3gp; *.3gp2; *.3gpp; *.amv; *.asf;  *.avi; *.bin; *.cue; *.divx; *.dv; *.flv; *.gxf; *.iso; *.m1v; *.m2v; *.m2t; *.m2ts; *.m4v; " +
                  " *.mkv; *.mov; *.mp2; *.mp2v; *.mp4; *.mp4v; *.mpa; *.mpe; *.mpeg; *.mpeg1; *.mpeg2; *.mpeg4; *.mpg; *.mpv2; *.mts; *.nsv; *.nuv; *.ogg; *.ogm; *.ogv; *.ogx; *.ps; *.rec; *.rm; *.rmvb; *.tod; *.ts; *.tts; *.vob; *.vro; *.webm";

        openFileDialog.Filter = formats;
        openFileDialog.ShowDialog();

It has to be exactly like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM