Results 1 to 7 of 7

Thread: [RESOLVED] [2.0] Always trigger AcceptButton even when another button has focus

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Resolved [RESOLVED] [2.0] Always trigger AcceptButton even when another button has focus

    I have a Windows form and a button that should always be triggered whenever the user presses Enter. I can set the AcceptButton property but if another button is clicked and keeps the focus, then this does no good.

    Is there a way to always click a certain button whenever the Enter key is pressed?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] Always trigger AcceptButton even when another button has focus

    In your form's keydown event, check to see if e.keycode = keys.enter. Then call Button5_Click()

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [2.0] Always trigger AcceptButton even when another button has focus

    Quote Originally Posted by mendhak
    In your form's keydown event, check to see if e.keycode = keys.enter. Then call Button5_Click()
    That only works when the form is first created and nothing has focus. If a button is ever clicked and gets focus, every Enter key press triggers the button click event.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Always trigger AcceptButton even when another button has focus

    It's not possible with a standard Button. Even if you set the form's KeyPreview property to True the form will not raise keyboard events for the Enter key when a Button has focus. I'm guessing that you would have to inherit the Button class and reverse the result of the IsInputKey function for the Enter key. I haven't checked but I'm guessing it returns True and you'd have to return False instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] Always trigger AcceptButton even when another button has focus

    OOh.. yes.

    Let's see what can be found...

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2.0] Always trigger AcceptButton even when another button has focus

    Code:
           protected override bool ProcessDialogKey(Keys keyData)
            {
                if (keyData == Keys.Enter)
                {
                    button3_Click(null, null);
                    return false;
    
                }
                else
                {
                    return base.ProcessDialogKey(keyData);
                }
                
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                Console.WriteLine("Button3");
            }

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Always trigger AcceptButton even when another button has focus

    Rather than this:
    CSharp Code:
    1. button3_Click(null, null);
    you should do this:
    CSharp Code:
    1. this.button3.PerformClick();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width