C#, do forms have an option to start with a certain button "selected"?
This was hard to explain, but say I have a form with a numeric up down. When the form starts, I want the number in the numeric up down to be highlighted meaning you can just press and number without clicking in the box and it will put it in there. How would I go about doing this?
EDIT: For some reason, doing .select() o开发者_JAVA技巧n the control with no parameters does select the control which is what I want. But using .select(0, 3) doesn't highlight the default "1" that is in the box. How do I highlight it?You can Focus()
the control and make the IsDefault
property true to fires when pressing enter or returns (from another controls, as you need).
Just set the control's focus in the form's OnLoad
event.
This should do the trick:
myNumericUpDown.TabIndex = 0;
myNumericUpDown.Focus();
myNumericUpDown.Select(0, myNumericUpDown.Value.ToString().Length);
Or swap out the select with:
myNumericUpDown.Select(0, 99);
Just put it in your form load :-)
EDIT:
The control also needs to have TabIndex set to 0
Just make sure no other control have TabIndex set to 0 :-)
You can start by putting focus on the numeric up down, that should let you do what you want.
Yes, you can call the Focus()
method to set focus to a specific control.
E.g. in the constructor call myNumericUpDown.Focus();
I think you can just use the .Focus() method on the button in your code behind.
During the form load event try something like this ...
yourControl.Focus();
After InitializeComponents() you can call numericUpDown.Focus().
Set the ActiveControl
property of the form and you should be fine.
this.ActiveControl = numericUpDown1
Inorder to get the text selected within the control, add this part
numericUpDown1.Select(0, numericUpDown1.Text.Length);
精彩评论