6 Replies - 10095 Views - Last Post: 11 May 2014 - 12:37 PM Rate Topic: -----

#1 cy12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-May 14

How to convert C# console app to Winforms or other GUI?

Posted 08 May 2014 - 09:06 AM

I have written a long console app (over 1000 lines of code) and I want it to have a GUI. I was wondering if there was a way to quickly convert it to Winforms or other GUI?

I already know how to include Winforms to a console app. But when I compile it, the Winforms does not include the Console app component of the program. My Form.cs includes the Winforms and Program.cs is the console app part with >1000 lines of code that I wrote before. Compiling it, only the part in the Form.cs runs. The Program.cs part doesn't run. For example, I have many "Console.Writeline" and "Console.Readline" parts in the Program.cs, but they run after I add the "Form.cs" to the program

Although the Form.cs runs, the Form.cs [design] doesn't show up

<Removed Github Link>

This post has been edited by macosxnerd101: 09 October 2014 - 01:14 PM
Reason for edit:: Removed Github Link


Is This A Good Question/Topic? 0
  • +

Replies To: How to convert C# console app to Winforms or other GUI?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15781
  • View blog
  • Posts: 63,246
  • Joined: 12-June 08

Re: How to convert C# console app to Winforms or other GUI?

Posted 08 May 2014 - 09:12 AM

Quote

I have written a long console app (over 1000 lines of code) and I want it to have a GUI. I was wondering if there was a way to quickly convert it to Winforms or other GUI?

The short answer is no. There isn't something you can chuck your console code into and have it spit out the right designer or GUI setup. You'll need to make logical choices on what GUI item is best represented where as well as what logic should be broken up in specific events to trigger.
Was This Post Helpful? 0
  • +
  • -

#3 TgaJava   User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 76
  • Joined: 28-April 14

Re: How to convert C# console app to Winforms or other GUI?

Posted 08 May 2014 - 09:17 AM

If i were you I just take the inputs required from the console. Then accept the inputs either using InputBox, or using the items in the toolbox. if you have anything to display, instead of using Console.Write, I take the string or variable to be displayed and give it to GUI components like textBox, label or MessageBox. The rest, like imports, the class construction is automatically done if you use IDE's like Visual Studio.

For further tutorial you can look at Greg Dolley’s Weblog
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to convert C# console app to Winforms or other GUI?

Posted 08 May 2014 - 05:06 PM

The biggest problem in the conversion will probably be application design.
Most console applications tend to be very linear: They run from start to finish. And they tend to not be very modular; IE they lack good use of methods to break down the work. Additionally there's almost no use of events.

Let's assume you've got some good design going in your console app and therefore have methods you can migrate.
What you should do, if you want to do this right, is to create a new WPF application (Not 1990's WinForms), then bring in your methods from the console app.
The better your console app was designed, the easier it will be to migrate to a GUI environment.

A tutorial that might help is:
My first C# application

As well as the WPF tutorials linked in my signature block
Was This Post Helpful? 0
  • +
  • -

#5 cy12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-May 14

Re: How to convert C# console app to Winforms or other GUI?

Posted 11 May 2014 - 10:27 AM

View PosttlhIn`toq, on 08 May 2014 - 05:06 PM, said:

The biggest problem in the conversion will probably be application design.
Most console applications tend to be very linear: They run from start to finish. And they tend to not be very modular; IE they lack good use of methods to break down the work. Additionally there's almost no use of events.


I have partially converted my console app to Winforms successfully. I put all the functions in the Console app into separate class files and have them called in the Form1_Load method in Form1.cs.

However, I am not getting Winforms to run the program linearly from start to finish, like the way it was in the Console App. I want to be able to display the Design of Form1.cs before running the rest of the program and having the MessageBox's showing up. However, what is happening is that the MessageBox's show up, then the rest of the program runs, and then the Design of Form1.cs shows up. If I called the methods in the classes in the Form1 constructor, it didn't make any difference. What should I do?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,565
  • Joined: 12-December 12

Re: How to convert C# console app to Winforms or other GUI?

Posted 11 May 2014 - 10:48 AM

This is precisely what tlhIn`toq is talking about. You need to re-think how the application will work.

In a console it is, as you say, linear. It runs from a known starting point to some end point, with a few loops thrown in.

With a GUI you:

  • Populate controls on the form with data
  • Display the window/form
  • Wait for the user to do something, such as pressing a Button (triggering an event)
  • You respond to these events either by changing something on the form, or perhaps by producing MessageBoxes
  • The application exits when the user decides to close the form.

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: How to convert C# console app to Winforms or other GUI?

Posted 11 May 2014 - 12:37 PM

Why do you even *have* messageboxes? You're building a GUI application. It is your way to tell something to the user. You don't need the messageboxes. Put a label on the form and update it.
Do you like (or even have) Windows applications that tell you every little detail by way of a messagebox? Hell no. They exist for specific things, like warning messages, or load file dialogs. Purposes that are meant to stop the user from working the program. But in a regular use that's unfriendly. You don't want to stop the user from using the program unless there is a problem.

Clearly you aren't reading about how to build or design a GUI based program. Nor are you bothering to read the MSDN on the purpose of the constructor or the Form_Load event.

The only thing that should happen in the constructor is the creation (construction, get it?) of the things required for the form to run.

The only thing that should happen in the form load, is the things that should happen as part of the loading process of the form the first time it is displayed (for example, reading a file containing its state when it was closed, so it can re-open to the same location).

Everything else takes place in response to the user. Your program does something when the user enters text in a textbox, or when the user clicks the [Calculate] button. Or when they select "Random" from a drop down combobox.

Have you actually *used* a Windows application? You should be trying to create a good user experience like those you have enjoyed and liked from other programs. If you wouldn't pay money for a program that works the way yours does, then you haven't done a very good job of thinking it through before typing your first line of code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1