Integrate a C# console application with a GUI

andres_v picture andres_v · Aug 11, 2016 · Viewed 21.2k times · Source

I've been developing using C# from scratch for less than 3 months and what I got at present is a console application made with Visual Studio 2015. This application consumes a web service, the XML is deserialized, formatted and saved in an Access database, and lastly a success or error XML is sent back to the web server. 3 classes make up the application: The main stream class, a SOAP client class and a DB class.

Now the reason why I'm posting this it's because I need to get that console app integrated with a GUI in which some data gotten from the deserialized XML should be shown (this is an extension of the project that the stakeholders didn't think about before stepping on the developing stage), but I have no idea how to do it.

So far I am reading about "Building WPF Applications" and "Windows Forms" but the documentation is overwhelming and I don't know if I'm on the right path, so can you guys give some guidelines before I start wasting time coding stuff that maybe are not the best option to integrate my console application with a GUI? Is there a way to make the console application become a GUI based application?

I would appreciate if you provide me with practical links to tutorials in which they quickly implement GUIs, my time is running out and I need to start coding right now. Thank you very much for reading this and helping me.

Answer

user2647513 picture user2647513 · Aug 11, 2016

If you're just looking for a barebones GUI and aren't too worried about it looking polished, I'd suggest you right click on your project -> add->Windows Form.

Then, turning your your Console App into a GUI based application is as simple as instantiating your Form-derived class and calling .ShowDialog().

Example:

using System.Windows.Forms;

//Note: if you create a new class by clicking PROJECT->Add Windows Form,
//then this class definition and the constructor are automatically created for you.
//Open MyFancyForm.cs, right click on the form, and click View Code to see it.
public partial class MyFancyForm : Form
{
    public MyFancyForm()
    {
        InitializeComponent();
    }
}

static void Main(string[] args)
{
     var guiForm = new MyFancyForm();

     guiForm.ShowDialog();//This "opens" the GUI on your screen
}

It's that simple. I suggest you add add a new class to your project as a WindowsForm instead of just a class so that you have access to the form designer. Of course, it's up to you to add fields to your form class and have it populate the GUI appropriately.

Edit: if you wish replace the Console entirely, right click on your project->properties and then change "output type" to Windows Application.