2013-03-13 87 views
3

我正在使用Visual Studio創建Windows窗體。我正在爲大學的作業製作基於文本的生存遊戲,但是我被卡住了。從Windows窗體上的文本框中獲取數據

我的目標是讓用戶在textbox中輸入一個選項(1-4)來做出選擇。

我嘗試使用4不同的按鈕,但似乎無法得到一個if語句來處理按鈕點擊。然後,我嘗試了一個帶按鈕的開關,然後是一個帶有文本的開關,現在帶有if/else if語句的文本框。

這裏是我的代碼:

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(); 
      } 
    } 
} 
} 

我希望有人可以提供幫助。我很高興與其餘的,只是如果聲明atm,它只是不會檢測textbox中的條目,我也想使用提交答案按鈕,但我不知道如何得到它的工作。

只是值得一提的,我不是一個高級用戶(如你可能知道)

回答

2

,我看到您使用的文本框和按鈕窗口的形式,但你正在閱讀從控制檯iChoice。如果你有textBox中的變量(iChoice),那麼這種方法試試這個iChoice = Int32.Parse(iChoiceTextBox.Text);

變化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(); 
     } 
    } 
+0

我忘了刪除檢測控制檯readline的,這只是一個嘗試出於絕望,我一直在自發布以來一直在使用我的程序,並使用與用於名稱的輸入框相同的輸入框,然後使用if else語句確定結果,這似乎目前工作正常。 – 2013-03-13 11:04:11

+0

@CainNeal如果這個答案沒問題,你應該接受它 – levi 2013-03-26 12:15:06