Solved

winform - default button

Posted on 2011-02-15
6
999 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 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