Specifying command-line arguments w/WinForms mangles Form

D

David Adams

Hi,

I have seen a million examples of using command-line parameters in the
following manner:

[STAThread]
static void Main(string[] args)
{
Application.Run(new frmMain(args));
}


public frmMain(string[] args):this()
{

if (args.Length>0)
{
if (args[0].ToUpper().Equals("PUT"))
m_AutomatedFTPMethod = FTPMethods.UploadingFile ;
else if (args[0].ToUpper().Equals("GET"))
m_AutomatedFTPMethod = FTPMethods.DownloadingFile ;
}
}

This actually works fine, but at some point, my form is wiped clean of all
controls and I am left with simply a blank form (plus any code I have
written). Is this structure only meant for Console applications? I've
also looked into the Environment.GetCommandLineArgs method, but I'm afraid
that may return different results based on the OS.

Any ideas?

Thanks,

David
 
M

Mike Kitchen

I expect this is because you have overridden the default constructor,
therefore the constructor for the parent.form class is not being called.
I could be wrong of course. Do you have a simple full app that you can share
that shows your problem.
Maybe with just a single button on it or something.

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
 
W

William Stacey [MVP]

You could test this by not using the constructor and using something like a
public static in frmMain.

public class FormMain
{
public static string[] Args = null;
public frmMain() {...}

static void Main(string[] args)
{
FormMain.Args = args;
Application.Run(new frmMain());
}

private void SomeMethod()
{
string[] args = FormMain.Args;
if ....
}
}
 
D

David Adams

Thank you both for your suggestions. William, your code snippet worked
perfectly and didn't "hose" my form! Thanks.

David
 

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