PDA

Click to See Complete Forum and Search --> : Newbie in C# Using Agruments


Iron Skull
Dec 3rd, 2006, 04:51 AM
Hi I need to call my Program like this:
C:\program.exe name

And I want to show the Agrument name in a label.
I tryed to do this like this:


namespace HelloWorld
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}

private void frmMain_Load(object sender, EventArgs e)
{
lblMsg.Text = "Hello" + e + "to this world!";
}
}
}


I figured out that this was wrong..
I didn't find anything on google.
And I asked my C# Teacher.. And she had NO CLUE were I was talking about..

(Ok, she is really a noob, she only knows or textbook inside - out but ok..)

Is here ayone how can help me out ^^?

penagate
Dec 3rd, 2006, 04:59 AM
The command-line arguments are not sent to any events, they are sent to the static Main() procedure of your startup class, if you define one that has a string[] argument.

class MyProgram
{
[STAThread] static void Main(string[] args)
{
// ...
}
}


You can also access them using the static Environment.GetCommandLineArgs() method.

lblMsg.Text = "Hello" + string.Join(", ", Environment.GetCommandLineArgs()) + "to this world!";

Iron Skull
Dec 4th, 2006, 09:12 AM
So in Program.cs I need to change:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}

into
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}

this

And then I need to send the var args to my Form1.cs?

Thanks ^^

penagate
Dec 4th, 2006, 09:17 AM
Yeah you can do that by passing them to the frmMain constructor or you can use Environment.CommandLine like I showed in my second example. Whichever way you choose is up to you.