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

View: New views
5 Messages — Rating Filter:   Alert me  

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

by Summer_Intern :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello ,

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.
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 !




_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: How to capture a 'Carriage Return' in a windows form ?

by briancurtin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jul 1, 2009 at 09:31, Sivaguru Perambalam <itssivaguru@...> wrote:
Hello ,

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.
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 !

You could check out the OnKeyDown event: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown(loband).aspx. The C# example pretty easily translates to IronPython, so you could follow that and check when the KeyEventArgs.KeyCode is equal to Keys.Return, then call your ButtonClick method.

_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: How to capture a 'Carriage Return' in a windows form ?

by Brannon Jones :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Set the AcceptsReturn property on the TextBox:

You'll also need to set the AcceptButton property on the Form:

Brannon

On Wed, Jul 1, 2009 at 07:41, Brian Curtin <brian.curtin@...> wrote:
On Wed, Jul 1, 2009 at 09:31, Sivaguru Perambalam <itssivaguru@...> wrote:
Hello ,

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.
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 !

You could check out the OnKeyDown event: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown(loband).aspx. The C# example pretty easily translates to IronPython, so you could follow that and check when the KeyEventArgs.KeyCode is equal to Keys.Return, then call your ButtonClick method.

_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: How to capture a 'Carriage Return' in a windows form ?

by Summer_Intern :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Thanks for the Suggestion . I set the 'AcceptsReturn = True' for the textbox.
But the problem I found is that its not able to recognize the 'Enter'key press .

Here is the Code :

        def OnKeyPress(self,sender,args):
                print (args.KeyChar)
                if(args.KeyChar == Keys.Return):
                        print 'Enter Key Pressed'

1) I tried args.KeyCode . It returned an exception
2) I tried if(args.KeyChar == Keys. Enter) , the if condition is not passing when I press the Enter Key . Where as a new line is printed on the console.
3) Same behavior when I have if(args.KeyChar == Keys.Return):

I think its not able to understand the enum 'Keys'.
I also tried doing 'from System.Windows.Forms import Keys'. but still no good .

Any idea whats missing or incorrect ?

Thanks !
briancurtin wrote:
On Wed, Jul 1, 2009 at 09:31, Sivaguru Perambalam <itssivaguru@gmail.com>wrote:

> Hello ,
>
> 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.
> 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 !
>

You could check out the OnKeyDown event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown(loband).aspx.
The C# example pretty easily translates to IronPython, so you could follow
that and check when the KeyEventArgs.KeyCode is equal to Keys.Return, then
call your ButtonClick method.

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Re: How to capture a 'Carriage Return' in a windows form ?

by Curt Hagenlocher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

It sounds like you're hooking the KeyPress event instead of the KeyDown event. That's why you get a KeyChar instead of a KeyCode and it also might be why the code isn't working as you expect.

I can get the Keys enumeration just fine:

IronPython 2.6 Beta 1 DEBUG (2.6.0.10) on .NET 2.0.50727.3053
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> clr.AddReference('System.Windows.Forms')
>>> from System.Windows.Forms import Keys
>>> Keys.Enter
<System.Windows.Forms.Keys object at 0x000000000000002B [Return]>
>>>



On Thu, Jul 9, 2009 at 1:32 PM, Summer_Intern <itssivaguru@...> wrote:

Hello,

Thanks for the Suggestion . I set the 'AcceptsReturn = True' for the
textbox.
But the problem I found is that its not able to recognize the 'Enter'key
press .

Here is the Code :

       def OnKeyPress(self,sender,args):
               print (args.KeyChar)
               if(args.KeyChar == Keys.Return):
                       print 'Enter Key Pressed'

1) I tried args.KeyCode . It returned an exception
2) I tried if(args.KeyChar == Keys. Enter) , the if condition is not passing
when I press the Enter Key . Where as a new line is printed on the console.
3) Same behavior when I have if(args.KeyChar == Keys.Return):

I think its not able to understand the enum 'Keys'.
I also tried doing 'from System.Windows.Forms import Keys'. but still no
good .

Any idea whats missing or incorrect ?

Thanks !

briancurtin wrote:
>
> On Wed, Jul 1, 2009 at 09:31, Sivaguru Perambalam
> <itssivaguru@...>wrote:
>
>> Hello ,
>>
>> 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.
>> 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 !
>>
>
> You could check out the OnKeyDown event:
> http://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown(loband).aspx.
> The C# example pretty easily translates to IronPython, so you could follow
> that and check when the KeyEventArgs.KeyCode is equal to Keys.Return, then
> call your ButtonClick method.
>
> _______________________________________________
> Users mailing list
> Users@...
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>

--
View this message in context: http://www.nabble.com/How-to-capture-a-%27Carriage-Return%27-in-a-windows-form---tp24290861p24417356.html
Sent from the IronPython mailing list archive at Nabble.com.

_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


_______________________________________________
Users mailing list
Users@...
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com