Solved

winform - default button

Posted on 2011-02-15
6
992 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now