views:

1096

answers:

4

I'm writing a console program that can accept 1 to 3 files. I'm using OpenFileDialog three times to accept the files, but the second time and third the file dialog is behind the console window, making it hard to notice. Any way to get it to appear above?

An image of the problem: http://img205.imageshack.us/img205/5312/problemr.png

The relevant code is:

static bool loadFile(ref List<string> ls)
{
 OpenFileDialog f = new OpenFileDialog();
 if (f.ShowDialog() == DialogResult.OK)
 {
  Console.WriteLine("Loaded file {0}", f.FileName);
  ls.Add(f.FileName);
  return true;
 }
 else
 {
  return false;
 }
}

[STAThread]
static void Main(string[] args)
{
 //sanity check
 if (args.Length > 3)
 {
  Console.WriteLine("Sorry, this program currently supports a maximum of three different reports to analyze at a time.");
  return;
 }
 else if (args.Length == 0) 
 {
   List<string> fL = new List<string>();
   for (int k = 0; k < 3; k++)
   {
     if (!loadFile(ref fL)) break;
   }
   if (fL.Count == 0)
   {
   InfoDisplay.HelpMessage();
   return;
   }
   else
   {
   args = fL.ToArray();
   }
 }

        //main program
A: 

Found a similar post here. Not tested, but give it a shot and let me know.

foreach(Process p in Process.GetProcesses)
{
    if(p.MainWindowTitle = <the main UI form caption>)
    {
     if(IsIconic(p.MainWindowHandle.ToInt32) != 0)
     {
      ShowWindow(p.MainWindowHandle.ToInt32, &H1);
      ShowWindow(f.Handle.ToInt32, &H1);
     }
     else
     {
      SetForegroundWindow(p.MainWindowHandle.ToInt32);
      SetForegroundWindow(f.Handle.ToInt32);
     }
    }
}
Matthew Vines
System.Windows.Forms.OpenFileDialog does not contain a definition for BringToFront....
James Reever
is that vb.net?
James Reever
Yeah, I just converted it.
Matthew Vines
I think I'm missing some references...system.threading?also, openfiledialog doesnt contain a definition for Handle.
James Reever
Hmm bummer, I'm not at my IDE sadly, just thought I would try to answer this one straight away. The only thing I can think to do would be create a form that shows the open dialog on Load, closes when the dialog closes, and passes the dialog result back. But that's really ugly and a hack applied to hack, so I really don't recommend it.
Matthew Vines
yeah, im not sure why this happens. the first openfiledialog appears just fine. also, i tried hiding the console window, but the 2nd and third openfiledialogs decide to have the lowest z-order of all my programs, so they can only been seen if i minimize everything...
James Reever
what's worse is that i know it can be done. i have a program called turbotop, which if you tell it the name of the openfiledialog, will make it appear on top. now if only i can do that natively in c#...
James Reever
+1  A: 

I know this doesn't directly answer the question, but the OpenFileDialog has a property called "MultiSelect" which indicates whether or not the user can select more than one file. Once the user does select the file(s), the property FileNames (string[]) gets populated with all the file names. You can then just do a check like this:

if(dialog.FileNames.Length > 3)
{
   //only 3 are allowed
}
BFree
right, but i'm afraid the user would not know that and just select a single file while wanting to load 2 or 3. or the files are in different locations, making it difficult to load all three withmultiselect.
James Reever
Fair enough, but from a user's perspective, I think getting prompted with three consecutive Dialog's would be quote annoying. Perhaps you can consider a form with three textboxes and three Browse buttons. I personally think that would be a bit more user friendly IMO.
BFree
well the thing is, i made a gui version and a console version.for some reason the console version is 10x faster despite using the same code for the main processing...
James Reever
+1  A: 

I encountered the same problem in a console application.

OpenFileDialog.ShowDialog can be called with a "parent window" handle argument. I create a dummy Form and use that as parent window argument (the dummy form is invisible, since I don't call Show() on it).

The following code works for me:

Form dummyForm = new System.Windows.Forms.Form();

OpenFileDialog myOpenFileDialog1 = new OpenFileDialog();  
if (myOpenFileDialog1.ShowDialog(dummyForm) == DialogResult.OK) {  
    fileName1 = myOpenFileDialog1.FileName;  
}

OpenFileDialog myOpenFileDialog2 = new OpenFileDialog();
if (myOpenFileDialog2.ShowDialog(dummyForm) == DialogResult.OK) {
    fileName2 = myOpenFileDialog2.FileName;
}
A: 

OpenFileDialog.ShowDialog can be called with a "parent window" handle argument. I create a dummy Form and use that as parent window argument (the dummy form is invisible, since I don't call Show() on it).

The Form is a bit heavy-weight, perhaps a label instead, since it just takes a IWin32Window?

OpenFileDialog.ShowDialog(new Label());

It seems to work.

If someone could come up with a reasonable solution to this (now-) resurrected question, that would be great.

Aaron

Limited Atonement