2017-01-05 13 views
1

フォームに4つのボタンがあり、ボタンの上、下、左、右をクリックして単語を入力したいと思っています。ボタンによるテキスト入力

Image of the example form

ユーザーが上下に行くことによって彼の文字、数字、記号を選択することができます。

左と右は単語の前後に行くためです。彼が現在の手紙を確認したい場合に備えて、彼は間違いを犯して前に進んだ。

これらは、これは私が今持っているものであり、それはしかし、作品

private int i; 

    public Form1() 
    { 
     InitializeComponent(); 
     tbOutput.Text = word;   
    } 

    private void btnUp_Click(object sender, EventArgs e) 
    { 
     //We don't want the i to go above the array 
     if (i <= 39) 
     { 
      i = i + 1; 
     } 
     tbOutput.Text = word + Convert.ToString(wordsAndLetters[i]);  
    } 

    private void btnDown_Click(object sender, EventArgs e) 
    { 
     //We don't want the i to go below the array 
     if (i > 0) 
     { 
      i = i - 1; 
     } 
     tbOutput.Text = word + Convert.ToString(wordsAndLetters[i]); 
    } 

    private void btnRight_Click(object sender, EventArgs e) 
    { 
     tbOutput.Text = word; 
     sb.Append(wordsAndLetters[i]); 
     word = sb.ToString(); 
     i = 0; 
     tbOutput.Clear(); 
     tbOutput.Text = word; 
    } 

    private void btnLeft_Click(object sender, EventArgs e) 
    { 
     int lengthword = sb.Length; 
     sb.Remove(lengthword -1, 1); 
     word = sb.ToString(); 
     tbOutput.Clear(); 
     tbOutput.Text = word; 
    } 
} 

など、今、私たちは、ボタンに文字の間に行くためのオプションを与える必要があり、変数

private string word; 

private StringBuilder sb = new StringBuilder(); 

private char[] wordsAndLetters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ',', '.', '!', '?' }; 

ですそれは完璧ではない。私の現在のプログラムをより良くする方法はありますか?

+0

'StringBuilder'と' .Append(char) 'を見てください。 –

答えて

0

既存の文字列に技術的に追加することはできません。既存の文字列は不変であるため、追加することはできません。代わりに、ここにいくつかの簡単なオプションがあります:

1)毎回新しい文字を連結します。ジェイはあなたのポストに彼のコメントで言及よう

word += wordsAndLetters[i]; 
//Which is the same as: 
//word = word + wordsAndLetters[i]; 

2)StringBuilderを使用してください。

StringBuilder sb = new StringBuilder(); 
sb.Append(wordsAndLetters[i]); 
word = sb.ToString(); 
+0

ありがとうございます。 私のコードを更新しました。それは今すぐに動作します:) – imsohateful

+0

素晴らしい@imsohateful! –

関連する問題