C Sharp

Discussion in 'NZ Computing' started by Shane, Sep 5, 2006.

  1. Shane

    Shane Guest

    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
     
    Shane, Sep 5, 2006
    #1
    1. Advertising

  2. Shane

    Phil Guest

    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
     
    Phil, Sep 5, 2006
    #2
    1. Advertising

  3. Shane

    Shane Guest

    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
     
    Shane, Sep 5, 2006
    #3
  4. Shane

    Phil Guest

    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
     
    Phil, Sep 5, 2006
    #4
  5. 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...
     
    Lawrence D'Oliveiro, Sep 5, 2006
    #5
  6. Shane

    David Guest

    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?
     
    David, Sep 5, 2006
    #6
  7. Shane

    Chris Lim Guest

    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?
     
    Chris Lim, Sep 5, 2006
    #7
  8. Shane

    Shane Guest

    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
     
    Shane, Sep 5, 2006
    #8
  9. Shane

    Chris Lim Guest

    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/article.asp?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.....
     
    Chris Lim, Sep 5, 2006
    #9
  10. In article <edj8d3$3a9$>, -a-geek.net 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
     
    Peter Huebner, Sep 5, 2006
    #10
  11. On Tue, 05 Sep 2006 19:19:52 +1200, 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?


    How about: myEnterButton.DialogResult = DialogResult.OK
     
    John A Ferguson, Sep 5, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Silverstrand

    Sharp develops 'two-way viewing' LCD

    Silverstrand, Jul 25, 2005, in forum: Hardware
    Replies:
    13
    Views:
    1,883
    XhArD
    Sep 12, 2005
  2. shiv
    Replies:
    0
    Views:
    2,859
  3. shiv
    Replies:
    0
    Views:
    788
  4. Jack Kop

    sharp organizer

    Jack Kop, Oct 6, 2003, in forum: Computer Support
    Replies:
    0
    Views:
    588
    Jack Kop
    Oct 6, 2003
  5. John Davis

    Sharp rintones

    John Davis, Nov 18, 2003, in forum: Computer Support
    Replies:
    1
    Views:
    447
Loading...

Share This Page