How can I set focus to TextBox in RunTime?
Hi,
I made a form with a TextBox within some Char and a Button , when you click Button, TextBox will be focused and all the text in this will be selected.
I tried to use Event Select of TextBox class, but it does not work.
Code:
txtBox1.select(); --> Its OK
txtBox1.select(1,30); ---> It does not work :(
Give me some advices. Thanks
Re: How can I set focus to TextBox in RunTime?
Firstly, Select is a method, not an event.
Secondly, the first overload of Select you have shown is inherited from the Control class and is described thusly in MSDN documentation:
Quote:
Activates the control.
That's obviously what you want. The second overload is inherited from the TextBoxBase class, which is the base class for the RichTextBox and MaskedTextBox too. It is described like so:
Quote:
Selects a range of text in the text box.
As you see, no mention of selecting the control, just text within the control.
If you want to focus a control then call its Select or Focus method. This is basically equivalent to the user clicking or tabbing to the control. If you select text within a TextBox then, by default, the effect will not be visible if the control doesn't have focus. If you want the selection to be visible regardless then you need to set the HideSelection property to False.
Re: How can I set focus to TextBox in RunTime?
Yeah, thanks so much. You are right.
I'm a amateur. I've just coded by C#.
I've done. But I don't know how to realize when a TextBox got Focus. I can not find out Method Get or Lost Focus of TextBox class. Do you know?
Re: How can I set focus to TextBox in RunTime?
You handle the Enter and Leave events of a control to be notified when it receives and loses focus. There are also GotFocus and LostFocus events that sound like the ones to use but they are for a very specific purpose. In almost all cases you use Enter and Leave.
Re: How can I set focus to TextBox in RunTime?