setting focus on a winform

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
AVL said:
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

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.
 
AVL said:
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.
 
Back
Top