views:

48

answers:

4

I have winform buttons that when you load the form, a certain button is selected. What I mean by selected, is that if "enter" is pressed, the button is pressed.

How can I change my buttons so they don't do this anymore?

+1  A: 

You need to set the acceptbutton on the form.

rerun
AcceptButton is set to (none), is there anything else that could be causing this?
Soo
Hum are you using any composite or third party controls.
rerun
I'm just working with WinForms built from scratch in Visual Studio. Yeah, it's strange, if I set AcceptButton, I can see it being set in my editor (the button is selected) and when I set it back to (none) there are no buttons selected, but when I run my program, buttons are selected when they shouldn't be...
Soo
+3  A: 

Your tab order is set in the order in which you add controls on the form. If your first control which can be pressed/selected/edited is that button which is getting pressed, the focus will automatically be on it when form is loaded.

You can cheat by setting the focus to some other control (maybe which is not visible? !hint * hint!) to avoid the button to be selected at first.

But also make sure tht button is not the AcceptButton of the form.

Nayan
Changing the TabIndex does not help my problem, I'll try manually setting the focus, thanks!
Soo
TabIndex is a complicated thing. If you try to edit it manually in properties, even then there is no guarantee that way it should work. Best way is to enable "Tab Order" in Edit menu in Visual Studio and start clicking in the order you want controls to be.
Nayan
+1  A: 

Two concepts have been touched on by Nayan and rerun:

1) AcceptButton 2) Tab order

There is one more I would add and then try to explain how the three things relate:

3) Focus

Focus means that a child control has the "keyboard focus". When a control has focus, it receives keyboard input and can respond to it. Focus is changed either by clicking a control with the mouse, or by using the Tab key.

Tab order is the order in which controls receive focus when the Tab key is pressed. It also determains which control initially gets focus (the first one in tab order).

The AcceptButton concept is a bit of a hybred. If a form's AcceptButton property is set to a button control, that button is pressed when the user presses the Enter key while focus is on any control that does not process the enter key itself. Typically the 'Ok' button on a form is set as the AcceptButton so that the user can enter data and press Enter as a shortcut to pressing the Ok button.

Tergiver
heheh.. i mentioned Focus too =))
Nayan
You know.. what I meant to do was tie each of those into how they affect the selected appearence of each control, but I completely failed to finish. Since the OP seems to have posted their own answer, I won't bother finishing it.
Tergiver
A: 

Element.Select() is what worked for me.

Soo