Задайте вопросЗадайте вопрос
 

ОтвеченоHow to capture a 'Carriage Return' in a windows form ?

  • 1 июля 2009 г. 14:40Summer Intern Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     
    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 !

Ответы

  • 1 июля 2009 г. 22:01Sebastian Dyck Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     ОтвеченоС кодом
    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!

Все ответы

  • 1 июля 2009 г. 22:01Sebastian Dyck Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     ОтвеченоС кодом
    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!
  • 10 июля 2009 г. 16:48Summer Intern Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     
    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