イベントを使用して、コンソールアプリケーションのようなKeyPress
新しいキャラクターを新しいに追加するプログラムがあります。Label
プログラムにメソッドを追加する必要があるInput
ため、関数を実行する代わりに Enter キーを押すと、文字列が返されます。イベントが文字列を返すようにしようとしましたKeyPress
が、明らかな理由で機能しません。これを機能させるにはどうすればよいですか?
注: 「文字列を返す」とは、つまり;
入力を待機するように要求する場合、Console
まだKeyPress
イベントを使用しますが、ユーザーからの文字列/入力を返します。
私がすでに書いたコードを理解していただければ幸いです。
私の KeyPress イベントハンドラ:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\b') // Backspace
{
if (ll.Text != "_" && ActualText != "") // Are there any characters to remove?
{
ActualText = ll.Text.Substring(0, ActualText.Length - 1);
ll.Text = ActualText + "_";
}
}
else
if (e.KeyChar == (char)13)
{
if (!inputmode)
{
foreach (KeyValuePair<string, Action> cm in Base.Command())
{
if (ActualText == cm.Key)
{
print(ActualText);
cm.Value();
}
}
}
else
{
inputmode = false;
lastInput = ActualText;
print("Input >> "+lastInput);
}
ActualText = "";
ll.Text = ActualText + "_";
}
else
if (!Char.IsControl(e.KeyChar)) // Ignore control chars such as Enter.
{
ActualText = ActualText + e.KeyChar.ToString();
ll.Text = ActualText + "_";
}
}