C# 从窗体上的文本框获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15382034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:36:02  来源:igfitidea点击:

Getting data from a textbox on windows forms

c#textbox

提问by Cain Neal

I'm using Visual Studio to make a windows form. I am making a text based survival game for an assignment at university, however I am stuck.

我正在使用 Visual Studio 制作一个 windows 窗体。我正在为大学作业制作一个基于文本的生存游戏,但是我被卡住了。

What I am aiming for is for the user to input a selection (1-4) into a textboxto make their option.

我的目标是让用户将选择 ( 1-4)输入到 a 中textbox以进行选择。

I tried using 4different buttons, but couldn't seem to get an if statement to work with the button clicks. Then i tried a switch with buttons, then a switch with text and now a textbox with if/else if statements.

我尝试使用4不同的按钮,但似乎无法使用 if 语句来处理按钮点击。然后我尝试了一个带按钮的开关,然后是一个带文本的开关,现在是一个带有 if/else if 语句的文本框。

Here is my code:

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;


namespace PF5
{
public partial class Form1 : Form
{

    int iScore = 0, iChoice;
    String sName;

    public Form1()
    {
        //asks user to input their name before the game begins
        sName = Microsoft.VisualBasic.Interaction.InputBox("Please enter    your name:", "What is Your Name?", "");
        //if no name is entered, they are asked again
        while (sName == "")
        {
            MessageBox.Show("Please enter your name.");
            sName = Microsoft.VisualBasic.Interaction.InputBox("Please enter your name:", "What is Your Name?", "");
        }


        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //displays the score
        lblScore.Text = iScore.ToString();
        //displays the scoring system in the Rules textbox
        txtRules.Text = "Welcome to SURVIVE,  " + sName + ", during your   time here you will be presented with numerous scenarios and you will be given FOUR options or choices." + Environment.NewLine + "" + Environment.NewLine + "For each Scenario there will be: " + Environment.NewLine + "" + Environment.NewLine + "ONE 'Perfect Answer' which will give you +2 points to your score. " + Environment.NewLine + "" + Environment.NewLine + "ONE 'Correct Answer' which will award you +1 point to your total. " + Environment.NewLine + "" + Environment.NewLine + "ONE 'Wrong Answer' which will award you +0 points." + Environment.NewLine + "" + Environment.NewLine + "ONE 'Fatal Answer' which will result in Game Over, Death and -1 point.";
    }


    private void btnExit_Click(object sender, EventArgs e)
    {
        // Display a message box asking users if they
        // want to exit the application.
        if (MessageBox.Show("Do you want to exit?", "SURVIVE",
         MessageBoxButtons.YesNo, MessageBoxIcon.Question)
         == DialogResult.Yes)
        {
            Application.Exit();
        }
    }

    private void btnStart_Click(object sender, EventArgs e)
    {

        //Displays story and Options for the user
        txtStory.Text = "After a heavy night in town, you wake up the following day with the worst headache ever. The night seems to be a blur. The usual business of outside seems to be quiter than normal, even for a Sunday, but you don't think too much about it." + Environment.NewLine + " After you have had a shower, a coffee and got ready you feel slightly better, you turn on the TV to watch your favorite channel, only to see some pandemic on the news, apparently a ZOMBIE OUTBREAK!" + Environment.NewLine + "Luckily for you though, you have played Black Ops Zombies and Minecraft for most of your student life so you know exactly what to do..."
            + Environment.NewLine + "" + Environment.NewLine + "Option 1: Run outside and ask the first person you see to help you" + Environment.NewLine + "" + Environment.NewLine + "Option 2: Take some time to get some food and money together and pack them along with extra clothes into your bag." + Environment.NewLine + "" + Environment.NewLine + "Option 3: Text your friends and see if they are okay." + Environment.NewLine + "" + Environment.NewLine + "Option 4: Call the police to let them know a Zombie Outbreak is happening";
        btnStart.Visible = false;

            txtChoice.Text = Convert.ToString(iChoice);
            iChoice = Convert.ToInt32(Console.ReadLine());
            if  (iChoice == 1)
            {
                    txtResult.Text = "You run outside and grab the first person you see by the shoulder, apparently this particular person just wanted to chomp on your flesh." + Environment.NewLine + "" + Environment.NewLine + "YOU DIED!!!";
                    iScore = -1;
                    lblScore.Text = iScore.ToString();
            }
    }
}
}

I hope someone can help. I'm pleased with the rest of it, just the if statement atm, it just wont detect an entry in the textbox, i was also thinking of using a submit answer button, but i have no idea how i would get that to work.

我希望有人能帮帮忙。我对它的其余部分感到满意​​,只是 if 语句atm,它不会检测到 中的条目textbox,我也在考虑使用提交答案按钮,但我不知道如何让它工作。

Just worth mentioning that i am not an advanced user (as you can probably tell)

值得一提的是,我不是高级用户(您可能会说)

采纳答案by pepela

As i see you are using windows forms with textBoxes and buttons, but then you are reading iChoicefrom console. if you have textBox for that variable (iChoice) then try this iChoice = Int32.Parse(iChoiceTextBox.Text);

正如我所看到的,您正在使用带有文本框和按钮的 Windows 窗体,但是您正在iChoice从控制台阅读。如果你有那个变量的文本框 ( iChoice) 然后试试这个iChoice = Int32.Parse(iChoiceTextBox.Text);

change btnStart_click event this way:

以这种方式更改 btnStart_click 事件:

private void btnStart_Click(object sender, EventArgs e)
    {

        //Displays story and Options for the user
        txtStory.Text = "After a heavy night in town, you wake up the following day with the worst headache ever. The night seems to be a blur. The usual business of outside seems to be quiter than normal, even for a Sunday, but you don't think too much about it." + Environment.NewLine + " After you have had a shower, a coffee and got ready you feel slightly better, you turn on the TV to watch your favorite channel, only to see some pandemic on the news, apparently a ZOMBIE OUTBREAK!" + Environment.NewLine + "Luckily for you though, you have played Black Ops Zombies and Minecraft for most of your student life so you know exactly what to do..."
            + Environment.NewLine + "" + Environment.NewLine + "Option 1: Run outside and ask the first person you see to help you" + Environment.NewLine + "" + Environment.NewLine + "Option 2: Take some time to get some food and money together and pack them along with extra clothes into your bag." + Environment.NewLine + "" + Environment.NewLine + "Option 3: Text your friends and see if they are okay." + Environment.NewLine + "" + Environment.NewLine + "Option 4: Call the police to let them know a Zombie Outbreak is happening";
        btnStart.Visible = false;

        iChoice = Int32.Parse(txtChoice.Text);
        if (iChoice == 1)
        {
            txtResult.Text = "You run outside and grab the first person you see by the shoulder, apparently this particular person just wanted to chomp on your flesh." + Environment.NewLine + "" + Environment.NewLine + "YOU DIED!!!";
            iScore = -1;
            lblScore.Text = iScore.ToString();
        }
    }