2013-08-05 5 views
10

私はTextBoxを持っていますが、ボタンを押したときに関数を呼び出す方法を説明するソースは見つかりません。Windowsフォームテキストボックスキーを入力

public Simple() 
{ 
    Text = "Server Command Line"; 
    Size = new Size(800, 400); 

    CenterToScreen(); 

    Button button = new Button(); 
    TextBox txt = new TextBox(); 

    txt.Location = new Point (20, Size.Height - 70); 
    txt.Size = new Size (600, 30); 
    txt.Parent = this; 

    button.Text = "SEND"; 
    button.Size = new Size (50, 20); 
    button.Location = new Point(620, Size.Height-70); 
    button.Parent = this; 
    button.Click += new EventHandler(Submit); 
} 

一部の情報源は機能を使用するように指示していますが、どのように呼び出されるのか分かりません。

+0

ユーザーがテキストボックスに何かを入力したときにボタンクリックイベントを呼び出すとしますか? –

+2

このページを最初に訪れ、周りを見て回ることをお勧めします:http://msdn.microsoft.com/en-us/library/vstudio/dd492171.aspx –

+0

あなたの質問を理解していません。 –

答えて

24

私が正しく理解した場合、テキストボックスに何かを入力しているときにユーザーがEnterを押したときにメソッドを呼び出すとしますか?もしそうなら、あなたはこのようTextBoxKeyUpイベントを使用する必要があります。

public Simple() 
{ 
    Text = "Server Command Line"; 
    ... 

    TextBox txt = new TextBox(); 
    txt.Location = new Point (20, Size.Height - 70); 
    txt.Size = new Size (600, 30); 
    txt.KeyUp += TextBoxKeyUp; //here we attach the event 
    txt.Parent = this;  

    Button button = new Button(); 
    ... 
} 

private void TextBoxKeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
     //Do something 
     e.Handled = true; 
    } 
} 
+0

代わりにKeyDownイベントを実行し、 'e.SuppressKeyPress = true; 'を追加すると、Enterキーでフォームがノイズを発生するのを防ぎます – hellyale

2

すでにこの

button.Click += new EventHandler(Submit); 

のように、同様のボタンを持っているあなたは、この関数を呼び出したい場合は、この

を行うことができます
button.PerformClick(); //this will call Submit you specified in the above statement 
関連する問題