In my C# WinForms program, I have a form that has only a single Button control on it.
By default, that Button control receives the focus on the form. But I don't want the Button to ever get focus.
Is there a solution, even one that would require a call to an unmanaged API?
In your form Load event you can set the focus on some other control.
If you do not want the control to ever get focus via keyboard, you can also set its TabStop property to false.
If you want that the button should not have focus when you open the form, then you need to correct the TabIndex property. The TabIndex property has an integer as value which specifies the order in which the controls get focus when tab key is pressed. If the control has TabIndex set to 0 then change it to some other value.
Check the documentation for TabIndex and TabStop properties on MSDN.
Use TabStop property of button
button1.TabStop = false;
Related
What determines the tab order when more controls have the same tabindex property?
For example, after adding a texbox to an empty form the texbox's tabindex was 0. By duplicating the texbox (Ctrl + mouse drag) the new textbox had tabindex 0 too. Then I added a button and its tabindex was 1.
After running the program the focus was on the 2nd textbox.
Then I changed button's tabindex to 0 so after that all controls had tabindex = 0 and after running the program the focus was on the button which was added last.
Does that mean that in case of multiple controls with the same tabindex property the tab order will be the opposite of order of adding the controls to the form? Seems as the tab order of the controls which share the same tabindex is just the opposite of their order of appearance in Form.Designer.cs file.
Or is it that button control always has priority over textbox control when they both share the same tabindex?
Is that documented somewhere?
From MSDN Control.TabIndex Property
A tab index can consist of any valid integer greater than or equal to
zero, lower numbers being earlier in the tab order. If more than one
control on the same parent control has the same tab index, the z-order
of the controls determines the order to cycle through the controls.
And a bit of a nuance as well:
For a control to be included in the tab order, its TabStop property
must be set to true.
So, you're seeing controls with the same TabIndex value tab in reverse order from the code behind file due to the z-index value.
I have a TextBox in WinRT which got the focus (Pointer focus) on load in a view in WinRT. I want to disable this and change the focus to unfocused at load. When I am in constructor of the view, the TextBox is Unfocused, but when I reach the event Loaded, the TextBox got, I don't know why, automatically, the focus at Pointer.
No instruction are done to put the focus on this control. I don't understand why it got the focus.
I try to change TabIndex, no success, the control got the focus again. When I try to put manually the focus on unfocused, I have an exception: "Value does not fall within the expected range.". I don't understand why I have this exception. I have only one control with the name that I gave to him.
Thank's for reply.
Solution 1:
You can set the textbox TabStop property to false.
Solution 2:
Set another controls TabIndex to a lower value than the TabIndex value of the texbox (the control you wish to recieve focus on startup).
I have a Windows Form with 27 controls, but I need the tab key to cycle between only four of these controls. Every time the tab key is pressed, it should only move the focus between these 4 controls.
How do I do this?
Set the TabIndex of the controls you wish to navigate through. For the others, set TabStop to false.
This article describes how to set the tab index on Windows Forms controls.
http://msdn.microsoft.com/en-us/library/bd16a8cw(v=vs.90).aspx
According to that article, setting the TabStop property to false, on the controls that you want to be ignored by TAB, should achieve your objective.
For the 4 controls that you do want to be affected by pressing TAB, use the TaxbIndex property to set the values from 0 to 3.
I create an user control that has some textbox and buttons.the problem is when I use tab to traverse in My form when my usercontrol get focus the focus go inside of user control and buttons insid if it would focus.How I can simply go to next control after my user control not inside it?
thanks
If the controls have Focusable CanHaveFocus TabStop sort of properties, set them to false.
Also set the TabIndex properties of those controls to 0. I'm sure that the controls will be ghosts in terms of getting tab focus.
Set TabStop property on your custom user control elements which you want to exclude (eg. your button) to false.
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?
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.
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.
You need to set the acceptbutton on the form.
Element.Select() is what worked for me.