# C# Question:  How do I declare a body?



## Crocodyle (Aug 4, 2009)

Well I recently began with Visual C# and thought "Wow, this is pretty interesting."
So then I started googling and learned that "Close()" closes the window and MessageBox.Show("Message of Pop up", "Title of Pop up"); ACTUALLY DOES WHATEVER YOU JUST READ.

So yeah, I'm pretty excited about this.

Three things that I can't fix though I've googled it alot are these errors:


> *Error 1:*'UserInterface.Form1.button3_Click(object, System.EventArgs)' must declare a body because it is not marked abstract, extern, or partial


In order of the columns the info is "Form1.cs" - Line 45 - Column 22


> *Error 2:*Program 'C:UsersAdministratorDocumentsVisual Studio 2008ProjectsUserInterfaceUserInterfaceobjDebugUserInterface.exe' has more than one entry point defined: 'UserInterface.Program.Main()'.  Compile with /main to specify the type that contains the entry point.


In order of the columns the info is "Program.cs" - Line 14 - Column 21


> *Error 3:*Program 'C:UsersCody SantosDocumentsVisual Studio 2008ProjectsUserInterfaceUserInterfaceobjDebugUserInterface.exe' has more than one entry point defined: 'UserInterface.Form1.ProcessStart.Main(string[])'.  Compile with /main to specify the type that contains the entry point.


In order of the columns the info is "Form1.cs" - Line 48 - Column 25

And just for the sake of helping you guys out, below is the actual coding of my project.


> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> ...



As you guys can see, I want to try launching different applications through this little application/program that I am trying to do.
All tips will be greatly appreciated.


----------



## FordGT90Concept (Aug 4, 2009)

Error #1:
The semicolon at the end of this line is invalid:

```
private void button3_Click(object sender, EventArgs e);
```

In this context, you must have brackets:

```
private void button3_Click(object sender, EventArgs e)
{
}
```

Error #2 & #3:
You have two "static void Main()" subroutines:
-Error 2 points at the Main in Program.cs.
-Error 3 points at the Main in Form1.cs.

Either you have to get rid of one of those or tell the compiler which to use via the Project properties (right-click on the project and select properties).


----------



## Crocodyle (Aug 4, 2009)

FordGT90Concept said:


> Error #1:
> The semicolon at the end of this line is invalid:
> 
> ```
> ...



Well on the internet I just found a work around for the whole "launch-the-app" thing.  This only works, however, by using a button and then using this code

```
System.Diagnostics.Process.Start("calc.exe");
```
to launch the application.

Is there anyway I can select the applications I want to launch, kind of like checkboxes and then press a button to launch all of the applications I selected?
What about choosing the application from a drop down menu, then click the button to launch it?
Thanks again


----------



## FordGT90Concept (Aug 4, 2009)

```
FileInfo fi = new FileInfo("PATH TO EXECUTABLE");
            ProcessStartInfo psi = new ProcessStartInfo(fi.FullName, "ARGUMENTS");
            psi.WorkingDirectory = fi.DirectoryName;   // A lot of applications get cranky without doing this.
            Process.Start(psi);
```

You can do "Process.Start(psi).WaitForExit();" if you want your app to wait until that application closes.


I attached an example that does what you ask.  Just look at the code in Main.cs to get an idea of how it works.


----------



## Kreij (Aug 4, 2009)

You can launch an external application from any method, and by using any type of control (ie. ComboBox, CheckBox, DataGridView, TreeView, Label, etc.) that you want as all of the controls inherit from the base Control class which encapulates (and exposes) the OnClick Event. Depending upon which control you use would depend on the best event to use.
For example 
ComboBox : SelectedIndexChanged
CheckBox : Checked or CheckedState
DataGridView : DoubleClick (of the Cell or complete Row)

You could even write your own custum user control to do it. (That's when the fun really starts )

Personally, I would decide how I wanted to present the options to the user first. The UI should be intuitive and easy to navigate.


----------



## FordGT90Concept (Aug 4, 2009)

Kreij said:


> ComboBox : SelectedIndexChanged


Oops, I should have done that in the example.


----------



## Crocodyle (Aug 5, 2009)

FordGT90Concept said:


> Oops, I should have done that in the example.



Does the example you provided only work for drop down lists?


----------



## FordGT90Concept (Aug 5, 2009)

It is designed for Combo Boxes, yes.  You need the same basic elements (path, arguments) to launch any application.  Instead of using the name/index of a Combo Box, just do:

```
if (chkMyCheckbox.Checked)
{
  // Same code as Combo Box
}
```


----------



## Crocodyle (Aug 5, 2009)

FordGT90Concept said:


> It is designed for Combo Boxes, yes.  You need the same basic elements (path, arguments) to launch any application.  Instead of using the name/index of a Combo Box, just do:
> 
> ```
> if (chkMyCheckbox.Checked)
> ...



When I put the same code as Combo Box, I put the code that matches up with the name of the program selected, correct?
Example:

```
{ "Calculator", Environment.GetEnvironmentVariable("SystemRoot") + @"\system32\calc.exe", "" },
```

Also, what is the code for the drop down boxes and when/where do I put them?


----------



## FordGT90Concept (Aug 5, 2009)

I attached an example with check boxes.  It has a LaunchApp function to handle launching the applications and also a button click event which checks if the check boxes are checked and taking appropriate actions if they are.

In both examples, most of the code is in Main.cs.  If you switch over to designer mode on Main.cs, you can click on the elements to see their properties.  In both examples, there really isn't much to see in designer mode except the name.  The names coorspond with what is on the Main.cs.


This code:

```
{ "Calculator", Environment.GetEnvironmentVariable("SystemRoot") + @"\system32\calc.exe", "" },
```
...was mostly for organization.  By altering that array, you could change which applications are available and how they behave without changing any other code.  You could do the same thing for checkboxes but it takes more work (the app would have to create and name checkboxes at runtime to corrospond with the indexes in that array).


----------



## Crocodyle (Aug 10, 2009)

Is there anyway I can make the application work over other applications?

Kind of like how On Screen Keyboard runs on/over other applications as well.


----------



## FordGT90Concept (Aug 10, 2009)

On the form in designer view, click on the form title bar, look at the properties toolbox, and set "TopMost" to true.


----------



## Crocodyle (Aug 10, 2009)

FordGT90Concept said:


> On the form in designer view, click on the form title bar, look at the properties toolbox, and set "TopMost" to true.



Thanks, but that only makes the application on top of the others. l want something that would in a way control the other applications. An example of this how On-ScreenKeyboard controls the application selected before it. Like you can open Notepad and then open On-ScreenKeyboard and then click on a 'U' then the U would appear on Notepad.

I want something similar to that.


----------



## FordGT90Concept (Aug 11, 2009)

That is really complex Windows shell stuff and could earn your application the "key logger" label from antiviruses...

You'll have to DllImport user32.dll especially the following two functions:

SendInput
GetAsyncKeyState


----------

