Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Computing > NZ Computing > C Sharp

Reply
Thread Tools

C Sharp

 
 
Shane
Guest
Posts: n/a
 
      09-05-2006
So Im bored as anything with programming atm
Im looking for help with a form, I have several buttons on the form, and I
want to wire one to the <enter> button. Every attempt I have made so far
the <enter> button gets wired to whichever button has focus, any tips?

--
Fry: Psst! Leela! You've got to get me out of here! It's horrible! Eating
scraps; letting my waste drop wherever it falls, like an animal in a zoo!
Leela: Animals go in the corner.
Fry: The corner! Why didn't I think of that?

blog: http://shanes.dyndns.org

 
Reply With Quote
 
 
 
 
Phil
Guest
Posts: n/a
 
      09-05-2006
Shane wrote, On 5/09/06 7.19 p:
> So Im bored as anything with programming atm
> Im looking for help with a form, I have several buttons on the form, and I
> want to wire one to the <enter> button. Every attempt I have made so far
> the <enter> button gets wired to whichever button has focus, any tips?
>


I haven't had to deal with this problem in a while, but the best
(possibly only) way to solve this is with javascript:

onkeydown="if ((event.which && event.which == 13) ||
(event.keyCode && event.keyCode == 13))
{document.theForm.theButton.click();return false;}
else return true;"

Add that to the <body> or <form> tag. Note that you'll want to change
theForm and theButton to the appropriate client ids generated by .NET,
so you'll probably want to generate and add the JS on the server-side.

-Phil
 
Reply With Quote
 
 
 
 
Shane
Guest
Posts: n/a
 
      09-05-2006
Phil wrote:

> Shane wrote, On 5/09/06 7.19 p:
>> So Im bored as anything with programming atm
>> Im looking for help with a form, I have several buttons on the form, and
>> I
>> want to wire one to the <enter> button. Every attempt I have made so far
>> the <enter> button gets wired to whichever button has focus, any tips?
>>

>
> I haven't had to deal with this problem in a while, but the best
> (possibly only) way to solve this is with javascript:
>
> onkeydown="if ((event.which && event.which == 13) ||
> (event.keyCode && event.keyCode == 13))
> {document.theForm.theButton.click();return false;}
> else return true;"
>
> Add that to the <body> or <form> tag. Note that you'll want to change
> theForm and theButton to the appropriate client ids generated by .NET,
> so you'll probably want to generate and add the JS on the server-side.
>
> -Phil


gah. I forget c sharp is pitched at the web
My form is a er um.. windows thingy
Ive tried tying the property in the form named AcceptButton to the button
that will always handle the <Enter> key
But when I click on that button, all that happens is button_click() is
called twice
*And* the <enter> button still clicks which ever button has focus at that
point
My other thought was to force the button I want tied to <enter> to have
focus, but to no avail

--
Kif: It's an emergency, sir.
Zapp Brannigan: Come back when it's a catastrophe!
[the ship rumbles]
Zapp Brannigan: Oh, very well...

blog: http://shanes.dyndns.org

 
Reply With Quote
 
Phil
Guest
Posts: n/a
 
      09-05-2006
Shane wrote, On 5/09/06 7.51 p:
> gah. I forget c sharp is pitched at the web
> My form is a er um.. windows thingy
> Ive tried tying the property in the form named AcceptButton to the button
> that will always handle the <Enter> key
> But when I click on that button, all that happens is button_click() is
> called twice
> *And* the <enter> button still clicks which ever button has focus at that
> point
> My other thought was to force the button I want tied to <enter> to have
> focus, but to no avail
>


Hmm, I'm not too familiar with winforms programming...

But, could you check to see if the <enter> key was pressed when a
button's event is executed, and if so, ignore it if it's not for the
button you want?

You also might be able to sub-class button, clear the Click event
handlers, and add your own which simply doesn't handle the <enter> key.

But I haven't don winforms programming in ages, so there's probably a
*much* better and cleaner way to do it.

-Phil
 
Reply With Quote
 
Lawrence D'Oliveiro
Guest
Posts: n/a
 
      09-05-2006
In message <edj8d3$3a9$>, Shane wrote:

> So Im bored as anything with programming atm
> Im looking for help with a form, I have several buttons on the form, and I
> want to wire one to the <enter> button. Every attempt I have made so far
> the <enter> button gets wired to whichever button has focus, any tips?


I know nothing about C-Sharp (C-Hash?), but in Tcl/Tk, I have done things
like this:

bind . <Return> {DoOKAction}
bind . <Escape> {DoCancelAction}

where the "." refers to the top-level (application) window, and the <Return>
and <Escape> are the appropriate keystroke events. These bindings are
global, not specific to any particular control.

Maybe that might ring some bells for you...
 
Reply With Quote
 
David
Guest
Posts: n/a
 
      09-05-2006
Shane wrote:
> So Im bored as anything with programming atm
> Im looking for help with a form, I have several buttons on the form, and I
> want to wire one to the <enter> button. Every attempt I have made so far
> the <enter> button gets wired to whichever button has focus, any tips?


Set the form's 'Default Button'/'Accept Button' or something like that
(don't remember the exact name) to the one you want?

 
Reply With Quote
 
Chris Lim
Guest
Posts: n/a
 
      09-05-2006
Shane wrote:
> Ive tried tying the property in the form named AcceptButton to the button
> that will always handle the <Enter> key
> But when I click on that button, all that happens is button_click() is
> called twice
> *And* the <enter> button still clicks which ever button has focus at that
> point


I'm no C# programmer, but it seems as though setting the AcceptButton
property should do what you want. Are you sure there's nothing else
going wrong that could be causing the above problem?

 
Reply With Quote
 
Shane
Guest
Posts: n/a
 
      09-05-2006
Chris Lim wrote:

> Shane wrote:
>> Ive tried tying the property in the form named AcceptButton to the button
>> that will always handle the <Enter> key
>> But when I click on that button, all that happens is button_click() is
>> called twice
>> *And* the <enter> button still clicks which ever button has focus at that
>> point

>
> I'm no C# programmer, but it seems as though setting the AcceptButton
> property should do what you want. Are you sure there's nothing else
> going wrong that could be causing the above problem?


Am I sure?
Absolutely NOT
But thats why programmings fun.. ish

My only wish is that the damn ide had betterer documentation, or that I
asked the right question

The AcceptButton property seems to allow me to use the keyboard (directly
onto my Display1.Text) but the enter button is stuck on the focused button
(which of course changes everytime I click a button)


--
Fry: It's like a party in my mouth and everyone's throwing up.

blog: http://shanes.dyndns.org

 
Reply With Quote
 
Chris Lim
Guest
Posts: n/a
 
      09-05-2006
Shane wrote:
> The AcceptButton property seems to allow me to use the keyboard (directly
> onto my Display1.Text) but the enter button is stuck on the focused button
> (which of course changes everytime I click a button)


Yeah I just tried it myself and you're right, pressing Enter causes the
button with focus to be clicked. It is standard Windows behaviour
though.

I found the following link that mentions the problem:

http://www.informit.com/articles/art...?p=31218&rl=1:

"If the Cancel button on the form gets the focus, then pressing Enter
causes that button to be pressed and, consequently, the CancelIt method
to execute. In other words, the button receives the input before the
form does. Unfortunately, there's no easy way to get around this. Some
controls, such as the Button and RichTextBox controls, automatically
handle the Enter key press when they have the focus, before anything
else can execute. As a result, once you give the Cancel button focus
(by clicking it, or tabbing to it), pressing the Enter key always
causes the CancelIt method to execute."

Apparently one way to get around that is to create your own custom
button.....

 
Reply With Quote
 
Peter Huebner
Guest
Posts: n/a
 
      09-05-2006
In article <edj8d3$3a9$>, says...
>
> So Im bored as anything with programming atm
> Im looking for help with a form, I have several buttons on the form, and I
> want to wire one to the <enter> button. Every attempt I have made so far
> the <enter> button gets wired to whichever button has focus, any tips?
>


You need to attach an event handler to the form, rather than the buttons.
So the form detects and processes key events before the buttons get to see
them. Then if the key detected is not the enter key, you either pass it on the
the control with focus or else call the [enter]button event directly.
If you have text boxes on the form, you may still need to pass the enter key on
to those, so you may need to test which object has focus as well.

NB - there are different key events that may be returned by the windows
functions library. You may get a raw code, you may get ascii with different
functions. At least that's how it works in Delphi ...

-P.

--
=========================================
firstname dot lastname at gmail fullstop com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sharp develops 'two-way viewing' LCD Silverstrand Hardware 13 09-12-2005 12:48 AM
how can I call a aspx page wth c sharp class ASP .Net 1 04-20-2005 08:54 PM
VB to C sharp anyone can point out which is the equivalent Hai Nguyen ASP .Net 3 01-08-2004 02:39 AM
Free exam simulation software for vb6,vb.net,c sharp , sql server , java , GRE and lot shiv MCSD 0 09-01-2003 06:10 AM
Free simulation exams for vb.net , c sharp , java , sql server , windows 2000 , GMAT shiv Microsoft Certification 0 08-07-2003 12:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57