Solved

winform - default button

Posted on 2011-02-15
6
1,003 Views
Last Modified: 2013-12-17
Hi,
I have a winform which has one button. How can I set as a default button
whenever user selects space bar or enter key?

Thanks,
JT
0
Comment
Question by:jtran007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 34900854
for enter key set the dialogresult OK Form Property to that button

otherwise you will have to enter the message pump to handle other keys
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34900880
sorry set the Accept button property to your preferred button
0
 
LVL 22

Expert Comment

by:p_davis
ID: 34900939
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 10

Accepted Solution

by:
Mez4343 earned 250 total points
ID: 34900962

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.AcceptButton = this.button1; // sets button1 as deault for Enter key
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)         
                button1_Click(sender, new EventArgs());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("button1 click event fired");
        }        
    }

Open in new window

0
 
LVL 22

Expert Comment

by:p_davis
ID: 34900980
which is effectively exactly what i just illustrated
0
 

Author Closing Comment

by:jtran007
ID: 34902856
Thanks,
JT
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question