1

イベントを使用して、コンソールアプリケーションのような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 + "_";
            }
    }
4

1 に答える 1

2

あなたの質問は少し不明確ですが、私がそれを正しく理解していれば、解決策は文字列を返すのではなく、イベントでは明らかにできない、KeyPress独自のイベントを発生させることです。

public delegate void EnterPressedHndlr(string myString);

public partial class Form1 : Form
{
  public event EnterPressedHndlr EnterPressed;

  void Form1_KeyPress(object sender, KeyPressEventArgs e)
  {
     //your calculation
     if (EnterPressed != null)
     {
        EnterPressed("your data");
     }
  }
}
于 2013-08-07T10:48:38.520 に答える