setting focus on a winform

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
 
D

DArnold

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.
 
H

Herfried K. Wagner [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

control focus 2
Win Form never gets focus?!? 1
TextBox Focus VS Select 4
DataGrid and Texbox Focus 1
Setting focus on a control 2
Textbox focus 2
TextBox lost focus 2
Custom user control and focus problem. 2

Top