Microsoft 开发人员网络 > 论坛主页 > Visual Studio Extensibility > How to capture a 'Carriage Return' in a windows form ?
提出问题提出问题
 

已答复How to capture a 'Carriage Return' in a windows form ?

  • 2009年7月1日 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 !

答案

  • 2009年7月1日 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!

全部回复

  • 2009年7月1日 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!
  • 2009年7月10日 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