Popping dialogs up on a console app

  • Thread starter Michael A. Covington
  • Start date
M

Michael A. Covington

In a console app, I can create and use FolderBrowserDialog and similar
objects, but they often pop up behind, rather than in front of, the console
window. What can I do about that? I understand
FolderBrowserDialog.Show( ) will take an argument to identify the parent
window, but I don't know what to put there.
 
Z

zacks

In a console app, I can create and use FolderBrowserDialog and similar
objects, but they often pop up behind, rather than in front of, the console
window.  What can I do about that?  I understand
FolderBrowserDialog.Show(  ) will take an argument to identify the parent
window, but I don't know what to put there.

Would not the parent be "this"?
 
Z

zacks

As I understand it, a console app doesn't have a "this."

Yeah, I forgot, you are right.

But getting back to the OP, I know I can popup a messagebox in a
console app as long as I add a reference for System.Windows.Forms, but
it appears that more is necessary to use the FolderBrowserDialog.
 
F

Family Tree Mike

As I understand it, a console app doesn't have a "this."

Yeah, I forgot, you are right.

But getting back to the OP, I know I can popup a messagebox in a
console app as long as I add a reference for System.Windows.Forms, but
it appears that more is necessary to use the FolderBrowserDialog.


Technically, you can use "this" in a console app, though I don't think it
helps the situation. For example, see this (pun intended...):

class Program
{
void Runner()
{
MessageBox.Show(this.ToString());
}

static void Main(string[] args)
{
Program p = new Program();
p.Runner();
}
}
 
J

Josh Einstein

Here, try the code below.

class Program
{
[STAThread( )] // important
static void Main( string[] args )
{

Application.EnableVisualStyles( );
Application.DoEvents( ); // initializes some windows forms
plumbing

var window = new ConsoleWindow( );
Console.WriteLine( window.Handle );

var dialog = new FolderBrowserDialog( );
dialog.ShowDialog( window );
Console.WriteLine( dialog.SelectedPath );


}
}

class ConsoleWindow : IWin32Window
{

public IntPtr Handle
{
get
{
return GetConsoleWindow( );
}
}

[DllImport( "kernel32.dll" )]
private static extern IntPtr GetConsoleWindow( );

}
 

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