Link to home
Start Free TrialLog in
Avatar of peer754
peer754

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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().
ASKER CERTIFIED SOLUTION
Avatar of Pryrates
Pryrates
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peer754
peer754

ASKER

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 ;)
no problem ;-). You were not the first and will not be the last that stumbles upon that bad windows behaviour.
Avatar of peer754

ASKER

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?
Avatar of peer754

ASKER

I made a call to the Dispose() method before initalizing the second dialog and that did the trick :D