简体   繁体   English

将C#控制台应用程序转换为C#Windows窗体应用程序

[英]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 . 我有一个简单的数字猜测控制台应用程序,我想将其接收并将其传输到Windows窗体应用程序。 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 窗口形式 编辑器的 快照器我知道如何创建一个窗口,但基本上我希望能够接受用户输入并让他们按Enter或返回,并继续在同一窗口中执行我的程序,而不必使用新窗口

First thing you will do is create winfows form app 您要做的第一件事是创建winfows form app

File > New > Project > Windows Forms Application 文件>新建>项目> Windows窗体应用程序

Main form designer window will open and on the left side you will have Toolbox 主窗体设计器窗口将打开,在左侧将有Toolbox

From Toolbox drag and drop TextBox to the window you just created and position it. 从“工具箱”中将“ TextBox拖放到刚创建的窗口中,然后将其放置。

After doing that, press that TextBox and on the right you will see property window. 完成后,按该TextBox,然后在右侧将看到属性窗口。

Inside property windows you will have 5 icons on top of it (Categorized, Alphabetic, Properties, Events, Property Page). 在属性窗口内,您将在其顶部有5个图标(分类,字母,属性,事件,属性页)。 Press Events and it will bring list of all events for selected element. 按事件,它将显示所选元素的所有事件的列表。

Find KeyDown event and double click on field next to it. 找到KeyDown事件,然后双击它旁边的字段。 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: 内部事件放置检查If user pressed enter然后在其中放入控制台应用程序中的逻辑:

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" 如果要与用户通信,可以从Toolbox拖放label ,并通过它与label.Text = "Hello user"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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