11

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.

andres_v
  • 399
  • 1
  • 4
  • 12

4 Answers4

5

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.

Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
4

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.

user2647513
  • 576
  • 1
  • 4
  • 13
  • Complete enough. So there's no way in .NET to prevent your console app from making GUI windows. You'd just get them both in that case: console window (first off) and GUI window (form) as you create it. I would add here that if you changed output type of your project from "Console Application" to "Windows Application", you'd get just GUI form without any console window so all the output of Console.WriteLines would disappear. That's the common way of making GUI apps with .NET. – tsul Aug 11 '16 at 18:19
  • Although OP did not specify that he wanted the Console hidden, I've modified my answer to include your suggestion – user2647513 Aug 11 '16 at 18:22
  • Thanks for your reply. I'm trying to follow up your suggestion: – andres_v Aug 11 '16 at 23:48
  • Thanks for your reply. I'm trying to follow up your suggestion: `project->add->Windows Form`, I've named it "MyFancyForm" (not really), instantiated the partial class as you code it within the namespace as the main stream class of the console application (literally in the same file). Nonetheless, VS throws the following error while underlining the line `public MyFancyForm()`: _Type MyFancyForm already defines a member called '.ctor' with the same parameter types_. Also pardon my ignorance but what would it be _InitializeComponent()_? I mean, should I set up stuff for the web form in there? TY. – andres_v Aug 12 '16 at 00:11
  • Ah, sorry, I believe my example was slightly misleading. When you added the form by clicking `project->add->Windows Form` VS automatically created the `public partial class MyFancyForm` code, so you don't have to manually enter that anywhere. To view the code for the form, double click on MyFancyForm.cs in the solution explorer, then right click on the form itself and click `View Code`. I'll edit my answer to make it more clear. – user2647513 Aug 12 '16 at 02:58
  • Thank you pal, you helped me. – andres_v Aug 12 '16 at 03:58
  • How do I pass parameters to the `guiForm.ShowDialog();` for the windows form to show them? Let's say a string or a list of strings. Right now I am sending a string but the VS shows this error: cannot convert from 'string' to 'System.Windows.Forms.IWin32Window'. I'm looking over the internet for workarounds but nothing seems to work. – andres_v Aug 12 '16 at 23:13
  • What exactly are you trying to get out of this GUI that's different from a regular Console? – user2647513 Aug 13 '16 at 00:51
  • Let me clarify: the new form you added to your project is a blank slate. It's up to you to add controls that do what you need it to do. Which isn't difficult, the hard part is making it look pretty. – user2647513 Aug 13 '16 at 01:57
  • All I have until now is a result of a process in a DB. After doing some processing on data and saving the results in an Access DB I need to show the names of some products, the strings I want to send to the windows form (could be one string or a list of them). The exact requirement is to show a checkbox besides the string, if the user checks the box and clicks a button, that string should dissapear from the form. You see, I am Back End PHP developer, I've never done Front End works (unless it has something to do with javascript) and even less in .NET, this is why I'm so lost here. Thanks again – andres_v Aug 13 '16 at 22:23
0

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

Community
  • 1
  • 1
Vincent L.
  • 46
  • 6
0

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...

JosephS
  • 744
  • 5
  • 22