2013-08-05 24 views
10

Possiedo un controllo TextBox, ma non riesco a trovare alcuna fonte che spieghi come chiamare una funzione quando si preme un pulsante.Windows Forms Textbox Tasto Invio

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); 
} 

Alcune fonti mi dicono di usare una funzione, ma non capisco come verrà chiamata.

+0

Si desidera chiamare l'evento clic sul pulsante quando l'utente digita qualcosa nella casella di testo? –

+2

Posso consigliare di visitare prima questa pagina e dare un'occhiata in giro: http://msdn.microsoft.com/en-us/library/vstudio/dd492171.aspx –

+0

Non capisco la tua domanda. –

risposta

24

Se ho capito correttamente, si desidera chiamare un metodo quando gli utenti premono mentre digitano qualcosa nella casella di testo? Se è così, è necessario utilizzare l'evento KeyUp di TextBox come questo:

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

Eseguendo invece l'evento KeyDown e aggiungendo 'e.SuppressKeyPress = true;' impedisce al modulo di creare rumore su Invio, quindi premere – hellyale

2

hai già un tasto, così come questo

button.Click += new EventHandler(Submit); 

se si desidera chiamare questa funzione si può fare questo

button.PerformClick(); //this will call Submit you specified in the above statement 
Problemi correlati