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
Solved

winform - default button

Posted on 2011-02-15
6
1,002 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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