How do I get arguments in a form application? - c#

I can find many examples on how to get arguments in a console application, but I can't seem to find an example of how to get arguments in a windows form application.
I would like to following things.
whenever I open a jpg file, windows launches my application.
I would like to know path and name of the jpg file from my application.
How do i do that?

Environment.GetCommandLineArgs

Open up program.cs, on a file > new > winform project, you'll get
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
change this to
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Now its just like the console apps, you'd access them via args.
Even if you don't go with this option, you should be aware of how the win form app is initialized :) This way, you could run different forms or not run a form at all.

Related

Generate lua parameters for C#

I made an application in C#, where I open a virtual keyboard. I also have a Lua application that runs this keyboard, and both are functional. In this scenario I needed to send some information from Lua to C# by the command line, but I'm not having success, does anyone know if it is possible, and if so, how could I do it?
Code Lua-
os.execute('C:\Users\Public\Documents\Keyboard.exe')
The code just calls the executable.
Code C#-
namespace WindowsFormsApp2
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
// Test if input arguments were supplied.
if (args.Length >= 0)
{
Console.ReadLine();
}
Console.WriteLine(args.Length);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Keyboard());
}
}
}
Here I read the console, but I'm not sending any code through Lua to read through C#, in this case I wanted to send a parameter through lua, and receive it through C#, but I don't know how to implement this through Lua.

How to show multiple forms simultaneously

I know this may have been answered, but I just can't find a suitable answer. Any idea how to show more than one Windows Form?
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main());
Application.Run(new MenuModule());
}
By declaring two Application.Run, the the Windows Form will show after I exit the first.
When you use that overload of Application.Run it will block until the form you passed has closed.
You could try:
new Main().Show();
new MenuModule().Show();
Application.Run();
This will run until you call Application.Exit even if both forms have been closed. So a better option might be:
new MenuModule().Show();
Application.Run( new Main() );
Which will cause the application to exit after the Main form has closed, regardless of the status of the MenuModule form.
As an option you could also have the Main form show the second one, that'll work too.
you can create an object in 'Main' class (form) contractor.
public main(){
.
.
.
new MenuModule().show();
.
.
.
}

Invocation error on running application then crashes c#

I have a Windows forms application, and for some reason I'm getting an invocation error on application.run form1 why would this be? I have been scratching my head all day
namespace UserDataImport
{
class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I'm getting a Target invocation error
start debugging the program, on the VS press Debug->Exception. that will open a window like this:
mark the "common Language Runtime Exceptions"
now when the exception is thrown you should have a nicer look at what went wrong. then add the code that went wrong

How to exit a Windows Forms/C# program

Class A has a Form1 (subclass of System.Windows.Forms.Form) member.
class A {
Form1 form;
public A()
{
form = new Form1();
form.Show();
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
A a = new A();
Application.Run();
}
The problem is I do not know how to exit the program. I have tried Application.Exit() when handling the Form.Closed event or call A.Dispose(), but the Windows Task Manager still lists the process of my program.
How do I finish this program?
Application.Run has 3 overloads. You are using this one with no arguments.
Windows runs your program in a message loop, but it doesn't care about your form.
So if you close your form it doesn't matter; the program will still run.
The second overload is what everyone uses, Application.Run(Form). This one runs a Windows message loop over your form, so when you click close on the window, the application closes.
Your code should be:
class A {
Form1 form;
public A()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form = new Form1();
form.Show();
Application.Run(form);
}
}
[STAThread]
static void Main()
{
A a = new A();
}
Following Microsoft you should use this:
Application.Run(a.Form);
Because MSDN states that
Most Windows Forms developers will not need to use this version of the method. You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed.
I think you have a mixup there. Check the documentation for Application.Exit.
There you will see that Exit will raise the Closed event for you, and calling Exit there might cause an infinite loop (which might be causing your problem, that the application is still visible).
Try this:
Environment.Exit(1);

What is the entering method of a multi form project?

I'm working with a C# project using System.Windows.Form to create the GUI, I have two forms within the VS project( MainForm and InitialPrompt). I've never used Forms before and Google hasn't been of much help.
Intended action:
InitialPrompt Load
Click Button on InitialPrompt
Load MainForm
However, since MainForm was created first there is some property/method that allows it to load first and the InitialPrompt does not load at all. How to I make MainForm the secondary form and InitialPrompt the primary?
Thanks in advance.
namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
You can change the above code to read
Application.Run(new Form2()); // or whatever the name of the second form is.
This is found in your Program.cs file.
Look for the Program.cs file inside your project. Inside you will see something like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
Just change new MainForm() to new InitialPrompt(). This will make InitialPrompt the main form.
Like any other .Net project it is the static void Main() method in a class defined in your project. Because of this, only one static void Main() method is allowed in a project.
NOTE: this static Main method must be defined as void return type, and it can either take no arguments, or it can be defined to take an array of strings to be passed as command line arguments.

Categories

Resources