Microsoft Developer Network > Página Inicial dos Fóruns > Visual Studio Extensibility > How to capture a 'Carriage Return' in a windows form ?
Fazer uma PerguntaFazer uma Pergunta
 

RespondidoHow to capture a 'Carriage Return' in a windows form ?

  • quarta-feira, 1 de julho de 2009 14:40Summer Intern Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Hello ,

    This is about a script written in IronPython2.0.1

    I have a Windows form with a text box and a button.
    What I want to implement it, when the user hits the 'Enter' key in the text box, the function that's linked to the ButtonClick event has to be invoked.
    I have TextBox.Multiline = False & TextBox.AcceptsReturn = False
    Please give me like a code example of how this could be done in a simpler way, since I am not familiar with Forms or GUI stuff .

    Thanks !

Respostas

  • quarta-feira, 1 de julho de 2009 22:01Sebastian Dyck Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     RespondidoContém Código
    Hello Summer Intern,

    Although you are refering to IronPython, this is more a WinForms related question - feel free to check out the corresponding forums . However - I bet that there exists a more elegant way - adding an EventHandler to the KeyPress Event of the TextField works.

    1. Go to your Form in the Designer mode.
    2. Select your TextBox.
    3. In the Properties window, switch to the Events section (-> Button with a flash on it).
    4. Double click the KeyPress Event in order to automatically generate and register a KeyPress EventHandler.
    5. Derive your custom code by the following example.

    private void textBox_KeyPress(object sender, KeyPressEventArgs e)
    {
      // 13: Numerical Representation of Carriage Return
      if (13 == e.KeyChar)
      {
        DoSomething();
      }
    }
    
    private void button_Click(object sender, EventArgs e)
    {
      DoSomething();
    }
    
    private void DoSomething()
    {
      // Do Something.
    }
    


    Unless you do not have any usage of the Sender and EventArgs arguments of the Button Click EventHandler, it's fine call your new method DoSomething() from both the Button Click EventHandler and the TextBox KeyPress EventHandler. Otherwise, call the Button Click EventHandler like button_Click(textBox, EventArgs.Empty); or similar.

    Kind regards
    Sebastian

    P.S.: All the best for your internship!

Todas as Respostas

  • quarta-feira, 1 de julho de 2009 22:01Sebastian Dyck Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     RespondidoContém Código
    Hello Summer Intern,

    Although you are refering to IronPython, this is more a WinForms related question - feel free to check out the corresponding forums . However - I bet that there exists a more elegant way - adding an EventHandler to the KeyPress Event of the TextField works.

    1. Go to your Form in the Designer mode.
    2. Select your TextBox.
    3. In the Properties window, switch to the Events section (-> Button with a flash on it).
    4. Double click the KeyPress Event in order to automatically generate and register a KeyPress EventHandler.
    5. Derive your custom code by the following example.

    private void textBox_KeyPress(object sender, KeyPressEventArgs e)
    {
      // 13: Numerical Representation of Carriage Return
      if (13 == e.KeyChar)
      {
        DoSomething();
      }
    }
    
    private void button_Click(object sender, EventArgs e)
    {
      DoSomething();
    }
    
    private void DoSomething()
    {
      // Do Something.
    }
    


    Unless you do not have any usage of the Sender and EventArgs arguments of the Button Click EventHandler, it's fine call your new method DoSomething() from both the Button Click EventHandler and the TextBox KeyPress EventHandler. Otherwise, call the Button Click EventHandler like button_Click(textBox, EventArgs.Empty); or similar.

    Kind regards
    Sebastian

    P.S.: All the best for your internship!
  • sexta-feira, 10 de julho de 2009 16:48Summer Intern Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Thanks Sebastian !

    One correction though .  I tried the proposed solution and it didnt capture the 'enter' key press.
    What works is ,

    if ( Keys.Enter == e.KeyCode) in the event KeyDown