Windows Forms – Passing Parameters at Runtime

misc_vol4_028 I received the following question over the weekend:

I’ve made a C# form application and I need to send a report name at runtime. How do I add an incoming parameter to the command line? Such as “crFORM.exe Shipform.rpt”

I have to assume the question relates more to how to retrieve the parameter in the code than how to pass it because the example shows how we’d pass it.

All WinForms csharp programs have a static method called Main() that looks something like this:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

If you are using visual studio 2008 to build your applications, this code can be found in Program.cs.

To accept parameters you will need to modify this method so that it accepts a string array as a parameter.  Most people name this parameter “args.”

[STAThread]
static void Main(String[] args)
{
    ...
}

If a parameter was passed, args will have a length greater than zero.  To retrieve the command line arguments all you need to do is retrieve the parameters out of args.

[STAThread]
static void Main(String[] args)
{

    if (args.Length > 0)
    {
        string arg1 = args[0];
        // do something appropriate with
        // arg1 here.
    }
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
}

Related Post

5 Responses to “Windows Forms – Passing Parameters at Runtime”

  • Rob W:

    Dave,

    Thanks a bunch – this is exactly what I need to move ahead with this project.
    What a great resource you’ve assembled here. No more google queries for me! :)

    Rob W
    Madison Heights, MI

  • Rob W:

    This works – I can display the report name now in the Program.cs section, however when I try to reference it in the Form1.cs I get the following error:

    my code:

    private void crystalReportViewer1_Load(object sender, EventArgs e)
    {
    crystalReportViewer1.ReportSource = Form1.ocrRpt;
    crystalReportViewer1.RefreshReport();
    }

    the error:

    ‘WindowsFormsApplication1.Form1′ does not contain a definition for ‘ocrRpt’.
    However I have this defined in Program.cs:

    static void Main(String[] args)
    {
    if (args.Length > 0)
    {
    string ocrRPT = args[0];

    How do I pass the definition from Program.cs to Form1.cs?

    Thanks

  • Rob W:

    note: I posted “Form1.ocrRPT” — but it was originally “ocrRPT” with the same error message. I also tried “Program.ocrRPT”.

    Keep in mind I am new to Visual Studio 2008.

    Thanks

  • Dave:

    You aren’t going to like my answer and I really don’t mean to insult you so I’m sorry if you take it that way but….

    You REALLY REALLY REALLY need to take a class in Object Oriented Programming. The question you ask is one you would not have to ask if you had a good understanding of object oriented programming in general and object oriented programming in C# specifically. You can get a good quality class from my buddies over at http://www.NewTechUSA.com. Tell Dan I sent you.

    Now that I have that out of the way.

    You’ll need to create a static property in Program.cs (Program is a static class that only accepts static properties and methods as members.) Once you have a static property, you can access it from any other code in your site as Program.PropertyName

    So, say you created a property called “ReportName” you’d access it as Program.ReportName in the rest of your code.

  • Rob W:

    No insult taken. I’m entirely new to Visual Studio 2008 – I’m using a trial version right now.

    I’m about a week into the process of creating a form to view a Crystal Reports report from a Progress database. It works great if I use a static reportname from within Form1.cs, but I wanted to get fancy and create one VS app for all reports. So sending a parameter became necessary.

    I like Visual Studio, and was told C# was the language to use. I have some projects coming up at work, and will have to evaluate if something I would normally do in Progress (Openedge v10) I can do better using Visual Studio. I am very familiar with Progress development, so that is the easy choice.

    If I were to get serious about VS 2008, I would get proper training. I checked out the training available at Newtech, and their prices are reasonable. I will tell them you sent me if I travel down that route.

    The stopping block for me may not be C# training, but simply that VS 2008 doesn’t provide a lot of hooks into any other DB other than MS SQL Server.

    That being said – I took your advice and created a property for Reportname and it worked great:

    ======================
    ….
    Program.cs
    ….
    public static string Reportname
    {
    get
    {
    return myReport;
    }
    }

    ….
    myReport = args[0];
    ….

    Form1.cs:
    ….

    crystalReportViewer1.ReportSource = Program.Reportname;

    ===============

    Thanks again for your help – I won’t bother you again until I’ve had some proper training.

    Best Regards,

    Rob W