【发布时间】:2018-03-03 05:08:07
【问题描述】:
我有一个简单的数字猜测控制台应用程序,我想将其传输到 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 编辑 我知道如何创建一个窗口,但基本上我希望能够接受用户输入并让他们按 Enter 或 Return 并继续在同一个窗口中使用我的程序而无需使用一个新的
【问题讨论】:
-
作为一个选项,您可以在表单上添加
TextBox,让他们在TextBox中输入数字,然后按Button确认输入的数字。 -
您可能希望对表单“事件”进行搜索,因为控制台不是事件驱动的。控制台只有一个输入……键盘。带有按钮、网格和文本框的表单有许多输入,它们通常由“事件”处理。有按钮、文本框等的事件……
-
对不起,我应该澄清一下,我确实打开了一个 winform 文件,并且在我的主 Application.EnableVisualStyles() 中有这 3 行; Application.SetCompatibleTextRenderingDefault(false);应用程序.运行(新 Form1()); .............................问题是我希望一切都按顺序发生它会发生在控制台中......我不希望每个事件都弹出一个新窗口......虽然感谢您的反馈!
-
谁说每次活动都会弹出一个窗口?
-
我将我的原始帖子编辑为......我希望更清楚我想要完成的工作
标签: c# .net winforms input console