将c#console app转换为c#windows form app

问题描述 投票:0回答:1

我有一个简单的数字猜测控制台应用程序,我想把它并将其转移到Windows窗体应用程序。我将输出更改为窗体,我知道如何更改窗口的外观(颜色,高度,宽度等),但我不确定如何从用户接收输入。任何指导都会有所帮助。我是一个新手,所以请原谅我的无知。下面是控制台脚本代码

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编辑我知道如何创建一个窗口但基本上我希望能够接受用户输入并让他们按回车或返回并继续我的程序在同一个窗口中不必使用新的

c# .net winforms input console
1个回答
0
投票

你要做的第一件事就是创造winfows form app

文件>新建>项目> Windows窗体应用程序

主窗体设计器窗口将打开,在左侧,您将有Toolbox

从工具箱中将TextBox拖放到刚刚创建的窗口并定位它。

完成后,按下TextBox,在右侧,您将看到属性窗口。

在内部属性窗口中,您将在其上面有5个图标(分类,字母,属性,事件,属性页)。按事件,它将显示所选元素的所有事件的列表。

找到KeyDown事件并双击它旁边的字段。它将自动在您的代码中创建事件,如下所示:

private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{

}

在事件内部检查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());
    }
}

就是这样。

如果你想与用户沟通,你可以从label拖放Toolbox并通过它与label.Text = "Hello user"进行通信

© www.soinside.com 2019 - 2024. All rights reserved.