CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2012
    Posts
    27

    Changing GUI to Console

    Hey everyone
    I'm sort of a newbie with C# and I have downloaded a server-sided game source that uses GUI's. Personally I don't think GUI's are suitable at all for a game server so I wanted to change that to a Console instead.

    So here is my problem. To run the server, you make a shortcut that opens the GUI with a configuration file. I tried changing this following code.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace ConsoleApplication1 {
    	static class Program {
    		public static frmMain MainForm { get; set; }
    
    		public static string IMGFilename { get; set; }
    
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main(string[] args) {
    			if (args.Length != 1) {
    				MessageBox.Show("Invalid argument length.");
    			}
    			else {
    				IMGFilename = args[0];
    				try {
    					Application.EnableVisualStyles();
    					Application.SetCompatibleTextRenderingDefault(false);
    					Application.Run(MainForm = new frmMain());
    					//Timer.Stop();
    				}
    				catch (Exception ex) {
    					MessageBox.Show(ex.ToString());
    					MainForm.Close();
    					Application.Exit();
    				}
    			}
    		}
    	}
    }
    I removed everything here and changed it to this, to test and see if it would work.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Derp");
                Console.Read();
            }
        }
    }
    When I double click on the compiled executable, nothing will open or anything.

    if anyone would like to provide assistance over either PM's or MSN it would be greatly appreciated!

    Thanks everyone!

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Changing GUI to Console

    What happens if you open a command line window and run it from there?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    May 2012
    Posts
    27

    Re: Changing GUI to Console

    Quote Originally Posted by BioPhysEngr View Post
    What happens if you open a command line window and run it from there?
    It still will not run. I get a message saying "Program1.exe" has stopped working.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Changing GUI to Console

    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    May 2012
    Posts
    27

    Re: Changing GUI to Console

    Quote Originally Posted by BioPhysEngr View Post
    Thank you! will try tommorow!

  6. #6
    Join Date
    Jan 2010
    Posts
    1,133

    Re: Changing GUI to Console

    Since that link is for C++, this is how you do it in C#:
    Open project properties, Application tab (the first one), and set Output Type to "Console Application" from the dropdown.

    Note, however, that there's more work to be done. In the original code, the line "Application.Run(MainForm = new frmMain())" starts a windows message loop, upon which all windowed applications are based. This is the underlying functionality which results, on the surface, in various events being raised (such as Load, Click, MouseMove, MouseDown, Paint, etc.). If the original app logic (or some of it) was based on handling WinForms events, then you have to recreate that functionality, since, in a console app, those events simply don't exist. But, since it's a server-side app, maybe the transition won't require too much work.

  7. #7
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Changing GUI to Console

    Thanks, Cthulhu! Should have read my link better first... :-)
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured