Hi,
I want to set focus on a textform when my form is displayed to the user.
I've set tablndex of textbox to 0 and tried the
textBox1.Focus()..but still the text in the textbox is not being
hightlighted...
how shud i proceed
DArnold - 11 Jun 2007 10:05 GMT
> Hi,
> I want to set focus on a textform when my form is displayed to the user.
[quoted text clipped - 3 lines]
>
> how shud i proceed
You go to the TextBox Properties/Events Icon/Focus/Enter and you give
the event a name like Textbox1_GotFocus or prefix it with whatever the
name of the textbox is. In the event, you call the TextboxHighlight
routine, giving it the Textbox control.
You can de-select the highlight by setting SelectionLength = 0
private void TextBox1_GotFocus(object sender, EventArgs e)
{
TextboxHighlight(textBox1);
}
private void TextboxHighlight(TextBox cntrl)
{
int ilen = cntrl.Text.Length;
cntrl.SelectionLength = ilen;
}
Then all you have to do is Textbox1.Focus, and if there is text in the
Textbox, it's going to be highlighted.
Herfried K. Wagner [MVP] - 11 Jun 2007 20:14 GMT
"AVL" <AVL@discussions.microsoft.com> schrieb:
> I want to set focus on a textform when my form is displayed to the user.
> I've set tablndex of textbox to 0 and tried the
> textBox1.Focus()..but still the text in the textbox is not being
> hightlighted...
In addition to the other reply, note that 'Focus' will fail if the form is
not yet visible. You may want to use 'Select' instead.

Signature
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>