2

I have a Windows Form application and I use a custom control that allows users to select and save image. However I need to insert some restrictions. The easy one was to select only jpg files but now I need to restrict the users to be able to see and select only jpg images with certain pattern in the name and if possible (I think I've seen this implemented in other windows applications) I want this pattern to stay in the File Name field and the user to not be able to delete it.

what I have done till now :

I have the restriction set:

    fileNameFilter = "All files (*.jpg)|" + ImageNameFilter + "*.jpg";
    openFileDialog1.Filter = fileNameFilter;

I can show the user what pattern he needs to look for:

openFileDialog1.FileName = fileNameFilter;

However there are two things that I still can't accomplish. Here is a print screen to make it clear:

enter image description here

First - the pattern is shown but I can delete it when it's made like this and I want if it's possible the File name field to be Read only or in other words the user should not be able to delete what I have put there.

Second - this is the list with previously opened files even though the file don't match the name restrictions/filters the user is still able to select form the list and save that file. I need, again if possible either to clean this list when the File Dialog is opened or somehow to stop the user from being able to save this file but i think the second will require too much extra business logic so I prefer just to clean the list.

2 Answers 2

4

Trying to control rigidly what's shown in a file dialog through a filter or a file name pattern is not going to work. The user can always type their own pattern into the file name edit box.

What you need to do is use the file dialog's facilities for controlling what objects are displayed. Unfortunately the C# wrapper doesn't expose this functionality that is offered by the underlying Win32 controls.

If you need to support XP, then you need to listen for the CDN_INCLUDEITEM notification. This is sent for each item in the folder. You therefore get the opportunity to either allow or deny the inclusion of each item.

For the dialogs used in Vista and later it's different. These dialogs use IFileDialog. You need to call the SetFilter method to add a filter. That filter is your implementation of IShellItemFilter which again controls inclusion using the IncludeItem method.

It's going to be a bit messy to make all this happen from C#, but this is the correct way to do what you ask. Once you do this, there's no need to even think about trying to make the file name edit box read-only. Because the dialog will only offer up the items that you have allowed to be included.

2
  • To be honest the things you've wrote are above my current level. I can try something, but there's a big chance to fail. You mentioned that if however if I succeed doing what you propose there's no need to try and make File Name edit box read only. But is there actually a way to do this, because it seems to me to be the easier solution?
    – Leron
    Feb 28, 2013 at 12:50
  • It's actually pretty easy to use IFileDialog Feb 28, 2013 at 19:42
2

No.

Two solutions: check the filename after Open-click (which you should do anyway), or create your own control that displays files that do match your filename pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.