File at second call to openfiledialog

Hi,
I'm building a win form application using C# VS 2010. In my main form (= the one that load at start up) Load() event handler I want user to point to some xml files needed. The first one is picked with this code:

            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Filter = "XML File|*.xml";
            fdlg.Title = "Open XML file containing program parameter structure!";
            fdlg.DefaultExt = ".xml";
            fdlg.FileName = Global.globalString + ".xml";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                string openedFile = fdlg.FileName;
                ix = openedFile.LastIndexOf("\\");
                Global.xmlPath = openedFile.Substring(0, ix);

                xmlReader = new XmlTextReader(openedFile);
                Global.mainXMLDoc.Load(xmlReader);
                xmlReader.Close();

            }
            else
            {
                MessageBox.Show("Failed to open " + Global.globalString + ".xml, please restart application!", "Error loading " + Global.globalString + ".xml", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }

Since I failed to use the same object (fdlg ) twice I tried to define another object and basically doing the same thing as above:

            OpenFileDialog fdlg2 = new OpenFileDialog();

            fdlg2.Title = "Open XML file containing saved filters!";
            fdlg2.Filter = "XML File|*.xml";
            fdlg2.DefaultExt = ".xml";
            fdlg2.FileName = "filter.xml";
            if (fdlg2.ShowDialog() == DialogResult.OK)
            {
                string openedFile = fdlg2.FileName;

                xmlReader = new XmlTextReader(openedFile);
                Global.filterDoc.Load(xmlReader);
            }
            else
            {
                MessageBox.Show("Failed to open Filter.xml, please restart application!", "Error loading " + Global.globalString + ".xml", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }

Now what happens at runtime is that the program hangs at the row  if (fdlg2.ShowDialog() == DialogResult.OK) i.e. ShowDialog method cannot be called twice even if it's used in seperate objects. Or???

BR,
Peer
peer754Asked:
Who is Participating?
 
PryratesCommented:
is it possible that the second file open dialog is "just" in the background?

I tried it and the seconds dialog open is not automatically the "top most" window. It is opend in the background so other windows (like the explorer) are in front of the dialog.

Try the following:
minimize ALL windows on your desktop and lauch your application (in built mode - run the "exe"-file).

0
 
käµfm³d 👽Commented:
ShowDialog() is a BLOCKING method, meaning that your application won't do anything until the dialog is closed. That said, are you sure the dialog isn't opening behind your form and you're just not seeing it? This would give the appearance of a program hang due to the blocking nature of ShowDialog().
0
 
peer754Author Commented:
Wow, that's what I call a huge embarrassment :-o ... Yes it was turning up beneath all my other opened windows, simple as that!

THANKS for opening my tired eyes ;)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
PryratesCommented:
no problem ;-). You were not the first and will not be the last that stumbles upon that bad windows behaviour.
0
 
peer754Author Commented:
So, isn't it any way I can make the second (why is the second filedialog not top-most?) file dialog top-most so that the user won't have to look for it behind all open windows he might have?

As far as I understand, there's no way to do this except writing your own openfiledialog class, right?
0
 
peer754Author Commented:
I made a call to the Dispose() method before initalizing the second dialog and that did the trick :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.