Integrate a C# console application with a GUI

28.6k Views Asked by At

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.

4

There are 4 best solutions below

10
On BEST ANSWER

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.

0
On

You can put a TextBox inside your form and redirect anything that would go in the console to the TextBox.

see this for more info Redirecting Console.WriteLine() to Textbox

0
On

Without entering in "why" territories, if you have a console application project you can right click the Project -> Properties -> Application and change the Output type to Class Library (or to Widows Application).

If you choose class library then you can consume your library from wherever.

See attached image...

0
On

What you're trying to achieve is actually relatively simple and straight forward. You need to launch your existing console application process from a new Windows Forms or WPF application and subscribe to its input and output streams.

If you don't want to show your current console application as a visible window, remember to set CreateNoWindow to true.

var processStartInfo = new ProcessStartInfo(fileName, arguments);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.CreateNoWindow = true;
// etc.

Here's an article that goes through all the steps: Embedding a Console in a C# Application

The above article's code can be found on GitHub if you want to take a closer look. Here's an example of what sort of embedding can be done:

enter image description here

With the project it's easy for you to either hide the whole console or have it side to side with some custom UI controls that do further processing with your data.