C#比较文本框

时间:2017-01-08 13:12:51

标签: c# winforms textbox

我正在编写一个基本游戏,程序从字典中获取一个随机单词并显示某些字母供玩家猜测。

它看起来像这样:G_m_,用户输入:Game - Correct

然而,正在发生的事情是,如果用户输入G_m_,答案仅显示正确,这与之前显示的完全相同。我知道这与下面的错误代码有关:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>

<div ng-app>
  <div ng-controller="myCtrl">
    <div ng-repeat="item in data" ng-if="showDiv($index)">
      {{item}}
    </div>
    <div ng-repeat="item in data">
      <span class="button" ng-class="{selected:!showDiv($index)}" ng-click="toggleDiv($index)">{{item}}</span>
    </div>
  </div>
</div>

在textbox4中我存储了播放器在textBox3中输入的内容我正在从字典中存储随机的部分单词。

 private void button2_Click(object sender, EventArgs e)
    {
        if (textBox4.Text == textBox3.Text)
        {
            timer1.Enabled = false;         
            timer1.Stop();                  
            MessageBox.Show("You Guessed The Word !");

有关如何比较textBox3中的原始单词而不是将部分单词与textBox4进行比较的任何想法?请记住,我仍然希望在textBox3中显示部分单词以供用户猜测?

TextBox3:G_m _

TextBox4:游戏

答案:正确

谢谢, [R

1 个答案:

答案 0 :(得分:1)

您应该存储字典中的完整单词(非部分)并将用户输入与完整单词进行比较:

private string word; // assign it when you get word from the dictionary

private void button2_Click(object sender, EventArgs e)
{
    if (textBox4.Text == word)
    {
        timer1.Enabled = false;         
        timer1.Stop();                  
        MessageBox.Show("You Guessed The Word !");
    }
}

还将textBox3(一个带有部分单词)设为只读,甚至用标签替换它。

并为控件使用一些有意义的名称。 E.g。

  • textBox4 =&gt; answerTextBox
  • textBox3 =&gt; hintTextBox
  • button2 =&gt; answerButton