繁体   English   中英

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

[英]Transforming c# console app to c# windows form app

我有一个简单的数字猜测控制台应用程序,我想将其接收并将其传输到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;
            }
        }
    } 

窗口形式 编辑器的 快照器我知道如何创建一个窗口,但基本上我希望能够接受用户输入并让他们按Enter或返回,并继续在同一窗口中执行我的程序,而不必使用新窗口

您要做的第一件事是创建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());
    }
}

就是这样。

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

暂无
暂无

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

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