Reputation: 2378
I have build an application using C# in Visual Studio 2010 and it is successfully running. Let's say the code filename is program.cs
I would like to build a simple GUI for the application so it is a bit more user friendly than command prompt.
Originally, the application will ask for user input in the command prompt. My goal is to allow user to use ComboBox and TextBox to provide input and hit a execute Button to start the program.
I tried Add New Item > Windows Form, but when I hit F5, the program still run in command prompt and looks like it just ignore the form I just created. When I comment out the whole program.cs file and run, I got an error saying there is no "Main()"
My question is: what is the best/easiest way to achieve my goal? what do I need to change in the code in order to link the form to the existing code?
Another note: Currently the user can take the executable run it anywhere. When the above goal is done, I would like to user to be able to take the form and run it anywhere. (I'm not sure if the executable will run form instead of command prompt, or something else. I'm not too familiar with this)
Upvotes: 1
Views: 10111
Reputation: 66469
First, add a project reference to the System.Windows.Forms
namespace.
Then open up program.cs
, reference the same namespace with a using
statement, and change the code to read:
static void Main()
{
Application.Run(new Form1());
}
After you add your UI controls to Form1
and wire everything up to get the values you want from the user, you can tie it into the logic that was in the Main()
method. How you do that is special to your case though. Try it first and reply if you get stuck.
And see the comment by @sa_ddam213 too. Just right-click the project in VS and select "Properties".
Upvotes: 7