Bring cursor to a textbox in C#
Solution 1
If that's a 'proper' TextBox (i.e. not custom) then simply calling Focus()
should work. It might not, however, if it's read-only (I'm not sure - I've not tried. I know you can get a caret in a read-only box, which implies it can get focus). Certainly if it's not Enabled
then you won't be able to set focus.
Check the CanFocus
property is true - if it's not, then there might be some other reason preventing the control from receiving focus.
If that's true
, however, and the caret still doesn't make it to the control - you need to verify that it is receiving it. Add an event handler for the text box's GotFocus
event and breakpoint it to clarify that it gets hit. My guess is that it your breakpoint will be hit. If so - then the answer is that another process is setting focus to another control immediately after your button click occurs. For example, if you do this kind of thing in a validation event handler you'll get a similar result, because the Windows Forms pipeline is already in the process of changing controls when the handler is fired.
Also - why are you setting TabIndex=1
? Generally TabIndex
is set at design time and left alone (unless of course these are dynamically created). Unless you have a particular reason for doing this I'd get rid of that line. It doesn't have a bearing on why this would/wouldn't work - just an observation.
Solution 2
Try textbox1.select()
. It's the best approach to bring your cursor to your textbox. It also selects content of the texbox which makes it easier for the user to edit what's inside the textbox.
Solution 3
Set theActiveControl property of the form
ActiveControl = yourtextbox
Solution 4
Edit again:
If you try to select a TextBox
in the Click
event of a TreeView
, it usually fails, because after the Click
event the TreeNode
will be selected, making your previous Focus()
or Select()
useless. The workable way is, perhaps, calling them in a Timer
.
Timer t = new Timer();
t.Interval = 10;
t.Tick += new EventHandler((s,ev)=>textBox2.Focus());
t.Start();
This is more like a hack though...
Solution 5
You have to use TextBox.Select Method
For example
textbox1.Select(textbox1.Text.Length,0);
Sets the cursor to the end of the text in yout textbox.
sanchop22
Updated on July 05, 2022Comments
-
sanchop22 almost 2 years
I want to bring the cursor to a textbox when i clicked a button. How can i do that? I tried Focus() method but it didn't not work. The code is shown below.
CsNIPAddrTextBox.Focus(); CsNIPAddrTextBox.TabIndex = 1;
-
sanchop22 almost 12 yearsNo, i want to move tab to the textbox. This only moves mouse pointer to textbox1 location.
-
sanchop22 almost 12 yearsCanFocus property is true, i checked it. Focus() does not work, cursor does not go to textbox when i clicked.
-
CSharpened almost 12 yearsHave you tried removing the control and double checking you add the correct control. A fresh text box control that has not been messed with should be capable of taking focus using .Focus(). If it still doesnt work then suggestion above for testing the GotFocus event is your next step.
-
Alvin Wong almost 12 years@petre have you checked my newest answer?
-
barlop over 7 years@blabla so why did you accept this answer then if you found it didn't work? Try Michay's answer and see if it works and if it does then you should accept it
-
Syed Irfan Ahmad almost 3 yearsI had a similar problem where focus was not shifted to a textbox on a tabpage. Setting that control as form's active control worked ... without calling Focus() and SelectAll()