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
我有一个简单的数字猜测控制台应用程序,我想把它并将其转移到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 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
Snobper of Window表单编辑我知道如何创建一个窗口,但基本上我希望能够接受用户输入并让他们按回车或返回并继续我的程序在同一个窗口中不必使用新的
1 个解决方案
#1
0
First thing you will do is create winfows form app
你要做的第一件事是创建winfows表单应用程序
File > New > Project > Windows Forms Application
文件>新建>项目> Windows窗体应用程序
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.
从工具箱中将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). Press Events and it will bring list of all events for selected element.
在内部属性窗口中,您将在其上面有5个图标(分类,字母,属性,事件,属性页)。按事件,它将显示所选元素的所有事件的列表。
Find KeyDown
event and double click on field next to it. It will automatically create event in your code like this:
找到KeyDown事件并双击它旁边的字段。它将自动在您的代码中创建事件,如下所示:
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:
内部事件放置检查如果用户按下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.Text =“Hello user”进行通信
#1
0
First thing you will do is create winfows form app
你要做的第一件事是创建winfows表单应用程序
File > New > Project > Windows Forms Application
文件>新建>项目> Windows窗体应用程序
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.
从工具箱中将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). Press Events and it will bring list of all events for selected element.
在内部属性窗口中,您将在其上面有5个图标(分类,字母,属性,事件,属性页)。按事件,它将显示所选元素的所有事件的列表。
Find KeyDown
event and double click on field next to it. It will automatically create event in your code like this:
找到KeyDown事件并双击它旁边的字段。它将自动在您的代码中创建事件,如下所示:
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:
内部事件放置检查如果用户按下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.Text =“Hello user”进行通信