textbox.Focus() not working in C#
56,175
Solution 1
Use Select()
instead:
recipientEmail_tbx.Select();
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
Solution 2
Add Delay some miliSec
. Delay then call Focus()
and Not forget to put inside Dispatcher
.
Task.Delay(100).ContinueWith(_ =>
{
Application.Current.Dispatcher.Invoke(new Action(() =>
{
TextBoxNAme.Focus();
}));
});
Solution 3
Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
Related videos on Youtube
Author by
Dark Knight
Updated on July 06, 2020Comments
-
Dark Knight almost 4 years
am wondering why this code fails to focus the textbox...?
private void sendEmail_btn_Click(object sender, EventArgs e) { String sendTo = recipientEmail_tbx.Text.Trim(); if (!IsValidEmailAddress(sendTo)) { MessageBox.Show("Please Enter valid Email address","Cognex" MessageBoxButtons.OK, MessageBoxIcon.Error); recipientEmail_tbx.Focus(); } }
-
Cody Gray over 13 yearsThe code you posted doesn't compile—the best overload match for the
MessageBox.Show
call has some invalid arguments. Once that's fixed (I specified a caption), the code works exactly as expected and sets the focus to the textbox. What's the problem? Do you want the text that has already been entered to appear selected? -
Dark Knight over 13 years@Cody:its edited...check now:)
-
Cody Gray over 13 yearsLike I said, even once I fixed the call to
MessageBox.Show
by adding a caption string (exactly the same thing you did), the code still works fine. After the message box is displayed, the focus is set back to the textbox. Why do you say it's "not working"? Were you hoping for the text that has been entered in the textbox to appear selected/highlighted? -
Dark Knight over 13 yearsno no...if cursor stops in text box thats enough...but its not working here..even I tried this.ActiveControl=recipientEmail_tbx...no luck :(
-
Daniel Peñalba over 13 years@Sisya: Did you tried Select() instead?
-
Dark Knight over 13 years@Daniel:yup...even its not working for me..horrible!!
-
Daniel Peñalba over 13 years@Sisya: mmm it seems to be other issue. Maybe when you are focusing the control, the form is not selected, or blocked by a background operation?
-
Dark Knight over 13 yearshow can form is not selected ?send button is in the same form
-
Cody Gray over 13 yearsI've tried several different things (like a default button on the form) to see if I can't throw a wrench into the operation, but I still can't repro the situation you describe. Can you give us any more information about your setup?
-
Alexandre Leites over 13 yearsHave you tried checking your recipientEmail_tbx's TabStop if it is set to
true
-
Dark Knight over 13 years@all:thanks for ur time...I fixed it by using this.Focus..:) MessageBox pop up was the culprit here..
-
-
Admin over 10 years
Error 1 No overload for method 'Select' takes 0 arguments
-
Alfie about 10 yearsThe parameterless overload has existed since 1.1: msdn.microsoft.com/en-us/library/…
-
hfrmobile over 7 yearsWhen Select() does not work for you: Often some other control takes the focus. So ensure that your call to Select() to your control is the last one.
-
Fabien Teulieres about 4 yearsWhile I don't like these hacky "add-a-delay-to-make-things-work" type of solutions, it did get me out of a bind on my multi-threaded C# app. So thanks and +1 for you good sir!
-
Kushan Gowda over 3 yearsUse Select() instead!
-
WiiLF over 2 yearsWorks great, thank you man