0

I started a project as a console app in visual studios and I'm now finding that I need a UI (form) but when I run the program it beings up a console. Is there anyway of having my Program.cs code from my console app run from a button event?

Spinet of my Code:

    using System;
using System.Net.Sockets;
using System.IO;
public class Program
{

    public static string name = null;
    public static string userloc = null;
    public static string protocol = null;

    public static int Portal = 43;
    public static string Address = "whois.net.dcs.hull.ac.uk";

    static void Main(string[] args)
    {


        for (int i = 0; i < args.Length; i++)
        {
            switch (args[i])
            {
                case "-h0":
                    protocol = "-h0";
                    break;

                case "-h1":
                    protocol = "-h1";
                    break;

                case "-p":
                    Portal = int.Parse(args[i + 1]);
                    ++i;
                    break;

Form Code:

namespace location
{
    public partial class Location : Form
    {
        public Location()
        {
            InitializeComponent();
        }

        private void updateBtn_Click(object sender, EventArgs e)
        {

        }
    }
}
Master Chef
  • 65
  • 1
  • 12
  • Would it be an option to have two programs (one console, one form)? And then the former launches the latter? – mjwills Jul 06 '18 at 04:10
  • Perhaps this question may help [How do I convert a .NET console application to a Winforms or WPF application](https://stackoverflow.com/questions/144701/how-do-i-convert-a-net-console-application-to-a-winforms-or-wpf-application) – DeanOC Jul 06 '18 at 04:13
  • mjwills that would not be ideal unfortunately, but thank you. – Master Chef Jul 06 '18 at 04:42
  • DeanOC that seems to be along the right path, however, I'm confused as to how I can have my button on my form run the Main() code. – Master Chef Jul 06 '18 at 04:45

1 Answers1

0

Most of what you require is at How do I convert a .NET console application to a Winforms or WPF application.

You have then asked in comments "how I can have my button on my form run the Main() code?". I'm assuming you know that the updateBtn_Click method will get executed when updateBtn is clicked, and I'm assuming that's the button you're referring to as "my button on my form".

The answer is that you wouldn't want to have the button click call Program.Main, since that is the entry point for the application.

I'm also assuming that you want to keep the command line parameters, so you need to leave the processing of args in Program.Main.

Whatever the rest of your functionality is, you could move it directly into your form, into the updateBtn_Click method. The code may need to be tweaked to get the values of the command line parameters from there by prefixing their names with Program. since they are all static (e.g. Program.Portal).

This will probably satisfy your requirements for now. Over time, you will realise that cramming code into "code behind" (which is what I've just told you to do) isn't a great idea. If you Refactor your code, then you would be able to use your functionality from the command line, or the WinForm app, or maybe a WPF or Web app. But maybe it's good enough for now...

Richardissimo
  • 5,596
  • 2
  • 18
  • 36