Files Disappear When Changing Filter in OpenFileDialog

M

marcus.kwok

I am having a weird problem and I have can't figure out why it is
happening. I create an OpenFileDialog and set a filename filter. When
the dialog first opens, the filter works correctly, and only the files
I want to see appear in the file list box. When I change the filter to
one of the other filters, all the files in the list disappear. If I
then manually type a filter into the "Filename" textbox, then the files
appear and the filter changes to the appropriate one. I am confident
that my code is correct, because when I paste it into a test program
the filter works exactly as expected (the file list updates as soon as
I select a different filter).


Here is the code that I am using:

OpenFileDialog* dlg = new OpenFileDialog();
dlg->Title = S"Choose Transmitter file";
dlg->InitialDirectory = S"input";
dlg->Filter = S"Transmitter files (xmtr*.dat)|xmtr*.dat|DAT files
(*.dat)|*.dat|All files (*.*)|*.*";
if (dlg->ShowDialog() == DialogResult::OK) {
textbox_xmtr->Text = dlg->FileName;
}


I have also tried it with a filter that only restricts on file
extension, i.e.,

dlg->Filter = S"DAT files (*.dat)|*.dat|All files (*.*)|*.*";

and I get the same result.

Has anybody seen this issue before?
 
M

marcus.kwok

I am having a weird problem and I have can't figure out why it is
happening. I create an OpenFileDialog and set a filename filter. When
the dialog first opens, the filter works correctly, and only the files
I want to see appear in the file list box. When I change the filter to
one of the other filters, all the files in the list disappear. If I
then manually type a filter into the "Filename" textbox, then the files
appear and the filter changes to the appropriate one. I am confident
that my code is correct, because when I paste it into a test program
the filter works exactly as expected (the file list updates as soon as
I select a different filter).

I should have mentioned that I am using VS .NET 2003. Also, I did
another test with interesting results.

If I change the filter, the files disappear. If I then browse to
another folder, then the filter is applied correctly. For example,

open dialog -> change filter -> files disappear -> click "parent
directory" button -> filter is applied and files are listed ->
double-click on original directory -> filter is still applied and files
are listed -> change filter again -> files disappear again ...
 
M

Marcus Kwok

I am having a weird problem and I have can't figure out why it is
happening. I create an OpenFileDialog and set a filename filter. When
the dialog first opens, the filter works correctly, and only the files
I want to see appear in the file list box. When I change the filter to
one of the other filters, all the files in the list disappear.

OK, I figured out how to reproduce this problem.

This is using Visual C++ .NET 2003 (Managed Extensions to C++, not
C++/CLI).

1. I created a new Visual C++ Windows Forms Application (.NET).

2. I added a button to this form.

3. I added an event handler for this button that consists of the
following:

OpenFileDialog* dlg = new OpenFileDialog;
dlg->Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
dlg->ShowDialog();

4. I added a native C++ file that consists of the following:

#include <iostream>

void f()
{
// Comment the following line to get correct behavior
std::cout << 0;
}

5. I turned off Precompiled Headers for the project. (not essential,
otherwise you need to add #include "stdafx.h" to the native file).

6. Compile and run (click the button then change the file filter).


Note that nowhere in the program did I actually call the function f().
It seems that its mere existence in the project is enough to mess this
up.

Can anybody else confirm this as a bug? Or did I not do something
properly, like wrap the native class somehow?
 
M

Marcus Kwok

Marcus Kwok said:
OK, I figured out how to reproduce this problem.

This is using Visual C++ .NET 2003 (Managed Extensions to C++, not
C++/CLI).

1. I created a new Visual C++ Windows Forms Application (.NET).

2. I added a button to this form.

3. I added an event handler for this button that consists of the
following:

OpenFileDialog* dlg = new OpenFileDialog;
dlg->Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
dlg->ShowDialog();

4. I added a native C++ file that consists of the following:

#include <iostream>

void f()
{
// Comment the following line to get correct behavior
std::cout << 0;
}

5. I turned off Precompiled Headers for the project. (not essential,
otherwise you need to add #include "stdafx.h" to the native file).

6. Compile and run (click the button then change the file filter).


Note that nowhere in the program did I actually call the function f().
It seems that its mere existence in the project is enough to mess this
up.

Can anybody else confirm this as a bug? Or did I not do something
properly, like wrap the native class somehow?

Hmm, I gave the source code and pre-compiled executables to my colleague
and the issue does not occur on his system. However, I tried it on a
different system and I DO get the bug (with the exact same pre-compiled
binary). Does anybody have any idea of how I can track down the source
of this problem?
 
M

Marcus Kwok

Marcus Kwok said:
OK, I figured out how to reproduce this problem.

This is using Visual C++ .NET 2003 (Managed Extensions to C++, not
C++/CLI).

1. I created a new Visual C++ Windows Forms Application (.NET).

2. I added a button to this form.

3. I added an event handler for this button that consists of the
following:

OpenFileDialog* dlg = new OpenFileDialog;
dlg->Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
dlg->ShowDialog();

4. I added a native C++ file that consists of the following:

#include <iostream>

void f()
{
// Comment the following line to get correct behavior
std::cout << 0;
}

5. I turned off Precompiled Headers for the project. (not essential,
otherwise you need to add #include "stdafx.h" to the native file).

6. Compile and run (click the button then change the file filter).


Note that nowhere in the program did I actually call the function f().
It seems that its mere existence in the project is enough to mess this
up.

Can anybody else confirm this as a bug? Or did I not do something
properly, like wrap the native class somehow?

Sorry to follow up to myself so many times, but I did find a workaround
to this problem that nobody else seems to have been able to reproduce.


If I set the project to "Not using managed extensions", but then only
enable it for the files that actually use them (plus add the appropriate
#using <mscorlib.dll>, #using <System.dll>, #using
<System.Windows.Forms.dll>, etc.), then the app behaves correctly.

I really wish this were documented somewhere, so I didn't have to waste
so much time hunting it down and figuring out the workaround. Of
course, I guess it would have helped if someone else were able to
reproduce the behavior I saw.
 
K

kael.cc

I'm also seeing this behavior just using the simple C# code:

OpenFileDialog d = new OpenFileDialog();
d.Filter="a (a*.*)|a*.*|b (b*.*)|b*.*";
d.ShowDialog();

This seems to obvious to be a bug. Could it be a system configuration
problem?
 
M

Marcus Kwok

I'm also seeing this behavior just using the simple C# code:

OpenFileDialog d = new OpenFileDialog();
d.Filter="a (a*.*)|a*.*|b (b*.*)|b*.*";
d.ShowDialog();

This seems to obvious to be a bug. Could it be a system configuration
problem?

It's possible, since it happens on my computer but not my colleagues,
and by the lack of responses here, not on anyone else's (besides yours).
I thought that maybe because I have both VS .NET 2003 and VS 6.0 on my
same machine that there is some conflict, but the code is using .NET
constructs, so AFAIK the 6.0 stuff shouldn't conflict. Also, the issue
happens on another machine that only has VS .NET 2003 on it (no 6.0).
 
V

V.Shiryaev

OpenFileDialog don't work with [MTAThread] attribute!

// weird problem with file and directories,
// then changing filter.
public class main
{
*[MTAThread]*
static void Main()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter="*.bmp (*.bmp)|*.bmp|*.jpg (*.jpg)|*.jpg";
openFileDialog.Title="Open File";
openFileDialog.ShowDialog();
}
}

// correct behavior
public class main
{
*[STAThread]*
static void Main()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter="*.bmp (*.bmp)|*.bmp|*.jpg (*.jpg)|*.jpg";
openFileDialog.Title="Open File";
openFileDialog.ShowDialog();
}
}
 
M

Marcus Kwok

V.Shiryaev said:
OpenFileDialog don't work with [MTAThread] attribute!

// weird problem with file and directories,
// then changing filter.
public class main
{
*[MTAThread]*
static void Main()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter="*.bmp (*.bmp)|*.bmp|*.jpg (*.jpg)|*.jpg";
openFileDialog.Title="Open File";
openFileDialog.ShowDialog();
}
}

// correct behavior
public class main
{
*[STAThread]*
static void Main()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter="*.bmp (*.bmp)|*.bmp|*.jpg (*.jpg)|*.jpg";
openFileDialog.Title="Open File";
openFileDialog.ShowDialog();
}
}

I think the problem may be deeper than this, since in my sample project
I have

System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;

as the first line of _tWinMain().

(this is MC++ not C# but it should be the same...).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top