简体   繁体   中英

Transforming c# console app to c# windows form app

i have a simple number guessing console app that i want to take it and transfer it to a Windows form app . I changed the output of to a windows form an i know how to change the look of the window (color , height , width , etc) but i'm not sure on how to receive input from user's. Any guidance would be helpful. I'm sort of a newbie so excuse me for my ignorance . Below is the console scrip code

class Program
    {
        static void Main(string[] args)
        {

         ;

            string appName = "Number Guesser";
            string appVersion = "1.0.0";
            string developer = "Jeffrey 'Jay-Dot' Pernia ";

            //change the color of the words //

            Console.ForegroundColor = ConsoleColor.Yellow;


            Console.WriteLine("{0}: version {1} by {2}", appName, appVersion, developer);


            //change color back to normal //

            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.White;

            Console.WriteLine("What is your name ");
            string input = Console.ReadLine();
            Console.WriteLine("Hello {0} lets play a game.... ", input);
            while (true)
            {
                Random random = new Random();

                int actualNumber = random.Next(1, 11);
                int guess = 0;

                Console.WriteLine("Guess a number between 1 - 10 ....bet you cant get it right!!");

                Console.ResetColor();
                while (actualNumber != guess)
                {
                    string userGuess = Console.ReadLine();

                    if (!int.TryParse(userGuess, out guess))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Put an actual number -_- ");

                        Console.ResetColor();

                        continue;
                    }

                    guess = Int32.Parse(userGuess);

                    if (guess != actualNumber)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Told you ...loser ! ");

                        Console.ResetColor();

                    }



                }
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Wow you're good !");
                Console.ResetColor();

                Console.WriteLine("Play again [Y or N]");
                string answer = Console.ReadLine().ToUpper();

                if (answer == "Y")
                    continue;
                else if (answer == "N")
                    return;
                else
                    return;
            }
        }
    } 

Snipper of Window form EDIT i know how to create a window but basically i want to be able to take the user input and have them press enter or return and keep going with my program in the same window w/o having to use a new one

First thing you will do is create winfows form app

File > New > Project > Windows Forms Application

Main form designer window will open and on the left side you will have Toolbox

From Toolbox drag and drop TextBox to the window you just created and position it.

After doing that, press that TextBox and on the right you will see property window.

Inside property windows you will have 5 icons on top of it (Categorized, Alphabetic, Properties, Events, Property Page). Press Events and it will bring list of all events for selected element.

Find KeyDown event and double click on field next to it. It will automatically create event in your code like this:

private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{

}

Inside event put check If user pressed enter and inside it you put your logic from console application:

private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
    {
        //Insert your logic here
        MessageBox.Show("Your result is: " + result.ToString());
    }
}

And that is it.

If you want to communicate with user, you could drag/drop label from Toolbox and communicate via it with label.Text = "Hello user"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM