文本框的位置

时间:2016-04-20 04:00:46

标签: c# textbox position

我想制作一款游戏,你写下你希望你的朋友在这里猜到的词 Form2

image

然后 Form3 会显示

image

我的问题是,当我点击按钮A 时,我希望在正确的位置中动态显示一个或一些文本框。 例如, Form2 中的单词是 BANANA ,当我点击A按钮时,它应该显示为A A A A 我在Form2中使用了这样的功能

public  void FindString(char Someword )
{

     int i = textBox1.TextLength;
     for (int j = 0; j < i; j++)
     {
        if ( Someword == textBox1.Text[j])
        {
                       //what to do here

        }
    }
}

然后我可以使用j的值来确定Form3中单词的位置(不能像示例那样处理多个位置)。

3 个答案:

答案 0 :(得分:1)

每次j上的char匹配时,都会将char追加到字符串中。每个不匹配都附加一个空白。加载字符串后,您可以清除并更新文本框。如果您希望它能够使用'form2'中的短语处理输入,那么您可以在将其发送到文本框之前检查空白。

String letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String integerToString(int n, int b) {
    return n < b 
           ? letters.charAt(n, b) // one digit
           : integerToString(n/b, b) + letters.charAt(n % b, b); // more than one
}

当然,这取决于您如何显示文字。

答案 1 :(得分:0)

也许你可以尝试这样的事情:

//New list to store pressed chars
List<char> usedChars = new List<char>();

public void FindString (char Somechar)
{
     //New StringBuilder to build final string
     StringBuilder sbWordIs = new StringBuilder();

     int i = textBox1.TextLength;
     for (int j = 0; j < i; j++)
     {
        if (Somechar == textBox1.Text[j])
        {
            //Store new correct char
            if (!usedChars.Contains(Somechar)) usedChars.Add (Somechar);
        }

        //Check whether each char in word was already pressed or not
        if (usedChars.Contains(textBox1.Text[j])) sbWordIs.Append(textBox1.Text[j]);
        else sbWordIs.Append ("_"); //If not, show an underscore
    }

    //Say yor new textbox is named "textBox2"
    textBox2.Text = sbWordIs.ToString();
}

(未测试的)

答案 2 :(得分:0)

 List<char>     guessWord = new List<char>(25);
        int i = textBox1.TextLength;
for (int j = 0; j < i; j++)
    {
        if (input == passWord[j])
        {
            guessWord.Insert(j, passWord[j]); //set j index of an array or     list to the corresponding character
        }
            else
    {
            guessWord.Insert(j,' ');  // or you can use an underscore _ to indicate that there should have been a letter there.  You could also add a line here to create a list or array containing wrong guesses and display those
        }
    }
        label1.Text = ""; //clear prior guesses

        foreach(char c in guessWord)
    {
        label1.Text += c;
    }
相关问题