Question on SelectAll

  • Thread starter Thread starter Wayne Wengert
  • Start date Start date
W

Wayne Wengert

In the GotFocus event for several text boxes I include code such as
"txtMyTxtBox.SelectAll". When I tab from textbox to textbox the text in the
textbox that got focus is selected as expected but if I use the mouse to
click on the textbox the text is not selected. I suspect this has something
to do with the order in which the various events fire? In VB6 this worked.

Does anyone have any suggestions regarding what is happening here and/or
ways to get the text selected when a mouse is used for changing focus?

Wayne
 
Hal;

I wasn't even aware of that. Let me try you suggestion. BTW, is LostFocus
still the best to use when leaving an object (I want to reset the background
color)

Wayne
 
Hal;

I tried using the enter event as you suggested (and I do see that there is a
corresponding Leave event) but although the event fires and the background
color of the textbox changes, it still does not select the text. In the code
below, the SetBackground sub simply changes the textbox's background color -
to help the user see what object has focus. If I walk through the code I can
see that the SelectAll is being executed but the text does not end up
selected - I'll do some digging about the enter event.

Private Sub txtAssocContactLastName_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtAssocContactLastName.Enter

SetBackground()

txtAssocContactLastName.SelectAll()

End Sub



Wayne
 
* "Hal Rosser said:
you should use the 'enter' event - not the gotfocus event

This doesn't make any difference, and there is IMHO no reason to prefer
'Enter' over 'GotFocus'.
 
* "Wayne Wengert said:
I wasn't even aware of that. Let me try you suggestion. BTW, is LostFocus
still the best to use when leaving an object (I want to reset the background
color)

That's the best way, IMO. You can use the 'Leave' event too. Be sure
you read and understood the docs on both events.
 
I looked up the events and, as suggested, either should work. According to
the Help, the events fire in the following sequence:

============= From Help Info ===============
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on),
by calling the Select or SelectNextControl methods, or by setting the
ContainerControl.ActiveControl property to the current form, focus events
occur in the following order:

Enter
GotFocus
Leave
Validating
Validated
LostFocus
When you change the focus by using the mouse or by calling the Focus method,
focus events occur in the following order:

Enter
GotFocus
LostFocus
Leave
Validating
Validated
==========================================

In my case, neither Enter nor GotFocus select the text - Weird?

Wayne
 
MAybe so - I was just going by MSDN recommendations to use 'Enter' over
GotFocus

Maybe if he adds 'selectall() ' to the click event as well ?? - would that
solve the OP's problem ?
 
* "Hal Rosser said:
MAybe so - I was just going by MSDN recommendations to use 'Enter' over
GotFocus

OK, you are right. There is a note in the docs.
 
For some reason - this will work sometimes: do focus() statement first
just before the SelectAll() statement, do a Focus() statement
 
Hal,

Thanks for that idea. It didn't work in my case. FYI, if I add another
SelectAll in the click event that does work - but I have over 25 forms, some
with 20+ text boxes - that is a lot of extra code. Do you know if this is
fixed in VSNET 2005?

Wayne
 

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

Back
Top