Arguments to windows app

  • Thread starter Thread starter Jay
  • Start date Start date
J

Jay

I have a windows app that looks like a dialog box.
It displaya a message, with Yes/No buttons.
And the Message can be different each time the windows app is started.
Can I pass in the message as an argument into the windows app, so that I can
display that?
 
I have a windows app that looks like a dialog box.
It displaya a message, with Yes/No buttons.
And the Message can be different each time the windows app is started.
Can I pass in the message as an argument into the windows app, so that I can
display that?

Change your Main method to the following:

static void Main(string[] args)

You can then read the args passed in and send them on to your form through
a property or method to be displayed. Ex:

string message = string.Empty;

if (args.Length > 0)
message = args[0];
Form1 frm = new Form1();
frm.CommandMessage = message;
Application.Run(frm);

This assumes you've added the string property CommandMessage to your Form1
class.
 
Sure. Let's assume your form is Form1 and the label you want to change based
on arguments is label1.

First you need to modify the static main method that studio geneted -
(in 2.0 this is in program.cs file, in 1.1 it's in the form1 itself).

Change:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
To:
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(args));
}

With that done overload the contructor of Form1:

public Form1(string[] args)
{
// Do Necessary Validations and Check before this line.
InitializeComponent();
label1.Text = args[0];
}
In other words - we take the arguments from Main - pass it to the forms
contructor and do whatever we want to do with the arguments in the forms
constructor.

I gave a quick test of this apporach on 2.0 - let me know if you're using
1.1 and have any problems.

Cheers,
rajiv.
 
Back
Top