Tuesday, 27 August 2013

Building GUI For Application

Building GUI For Application

First of all, here is the simple application I build using C# in Visual
Studio 2010, the filename is program.cs, all process will be displayed in
command prompt.
public static void Main(string[] args)
{
int input = Convert.ToInt32(Console.ReadLine());
switch (input)
{
case 1:
Console.WriteLine("A");
break;
case 2:
Console.WriteLine("B");
break;
case 3:
Console.WriteLine("C");
break;
default:
Console.WriteLine("default");
break;
}
}
I want to build a GUI to make it more user friendly.
I created a form with a ComboBox, a Label, and a Button. The values in the
ComboBox are [1,2,3,default]. I want to let the user select a value in the
ComboBox, hit the Button, and the program will update the label to
[A,B,C,default].
How can I keep the logic in program.cs, and achieve the above goal?
I created a form and visual studio generate a Form1.cs that looks like this
namespace quickTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
So I think the problem I ran into is I don't know how program.cs can
get/set value of Form1
In Main(), I added Application.Run(new Form1()); so it runs the form
instead of command prompt, but then I'm stuck. I tried
comboBox1.SelectedValue but I can only get value in From1.cs and not
program.cs, I need it to be in program.cs so I can apply the logic.
Just for clarification, this is just a sample I build. The actual
program.cs contains a lot more logic but I don't think it's affect what I
want to do here so I didn't put it in the description. I need a way to get
and set value from program.cs to the form.

No comments:

Post a Comment