sora0419
sora0419

Reputation: 2378

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.

Upvotes: 0

Views: 495

Answers (2)

Lathejockey81
Lathejockey81

Reputation: 1228

I don't believe the best solution is adding a GUI to a console application, but I've been in a similar situation before and was able to do it successfully. The best option would be to refactor the logic into libraries that could be referenced from a separate GUI application.

Create events in the form class and subscribe to them from program.cs to drive the logic that needs to happen. You can pass values to and from the logic with your EventArgs class. That is essentially what you do when you write the code that drives a form, you're just offloading it to a separate class.

Update: Added Example Code

This is basic event-based programming. Through the use of generics we can greatly reduce the amount of boilerplate code, but it would be good to get an understanding of the delegates we're creating automatically through generics. Shortcuts can be a hindrance if you don't understand how they work (or don't) when bugs arise.

Events how to: http://msdn.microsoft.com/en-us/library/w369ty8x.aspx

Generic delegates: http://msdn.microsoft.com/en-us/library/sx2bwtw7.aspx

For an example I created a pair of forms. MainWindow has a textbox OutputBox, and DetachedForm has a combobox OptionComboBox and a button TriggerButton, which we will use to fire the event.

MainWindow Class:

public partial class MainWindow : Form
{
    public MainWindow()
    {
        InitializeComponent();
        DetachedForm detachedForm = new DetachedForm();
        detachedForm.SelectionMade += new EventHandler<SelectionMadeEventArgs>(detachedForm_SelectionMade);            
        detachedForm.Show();
    }

    void detachedForm_SelectionMade(object sender, SelectionMadeEventArgs e)
    {
        OutputBox.Text = e.ActualSelection;
    }
}

DetachedForm Class

public partial class DetachedForm : Form
{
    public event EventHandler<SelectionMadeEventArgs> SelectionMade;

    public DetachedForm()
    {
        InitializeComponent();
    }

    private void OnSelectionMade(SelectionMadeEventArgs e)
    {
        EventHandler<SelectionMadeEventArgs> handler = SelectionMade;

        if (handler != null)
        {
            handler(this, e);
        }
    }

    private void TriggerButton_Click(object sender, EventArgs e)
    {
        if (OptionComboBox.SelectedItem != null)
        {
            SelectionMadeEventArgs args = new SelectionMadeEventArgs(OptionComboBox.SelectedItem.ToString());
            OnSelectionMade(args);
        }
    }
}

public class SelectionMadeEventArgs : EventArgs
{
    public SelectionMadeEventArgs(String actualSelection)
    {
        ActualSelection = actualSelection;
    }

    public String ActualSelection { get; set; }
}

Upvotes: 2

ferd tomale
ferd tomale

Reputation: 903

You can expose a public function or property in Form1.cs that gets/sets the value of the combo box, then in program.cs you can access that function to set or get the combo box.

Upvotes: 0

Related Questions