MSDN >
フォーラム ホーム
>
Visual Studio Extensibility
>
How to capture a 'Carriage Return' in a windows form ?
How to capture a 'Carriage Return' in a windows form ?
- 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 !
回答
- 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!- 回答の候補に設定Sebastian Dyck 2009年7月3日 7:15
- 回答としてマークRong-Chun ZhangMSFT, モデレータ2009年7月7日 9:56
すべての返信
- 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!- 回答の候補に設定Sebastian Dyck 2009年7月3日 7:15
- 回答としてマークRong-Chun ZhangMSFT, モデレータ2009年7月7日 9:56
- 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

