?
Solved

winform - default button

Posted on 2011-02-15
6
Medium Priority
?
1,008 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 10

Accepted Solution

by:
Mez4343 earned 1000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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