Click to See Complete Forum and Search --> : SetFocus problem


Cakkie
March 27th, 2001, 04:06 AM
There's something weird going on, again.
I have some code in the validate event of a listbox. Depending on the choise several textboxes will change from disabled to enabled.
The textbox is the next in the tabindex, but the focus goes to the first enabled element in the tabindex (that is, before the textbox was enabled).
When I then use the SetFocus to force the focus to the enabled box, the focus still ends up at the wrong control:

this examples uses 3 textboxes, text2 is disabled, text3 is enabled
the tabindex for text1 = 1, text2 = 2, text3 = 3.
it should go from 1 to 2 to 3, but it doesn't

private Sub Text1_Validate(Cancel as Boolean)
If text1.text <> "" then
text2.enabled = true
else
text2.enabled = false
end if
End Sub



When using this code, it goes from text1 to text3, even when there's text in text1.

private Sub Text1_Validate(Cancel as Boolean)
If text1.text <> "" then
text2.enabled = true
text2.setfocus
else
text2.enabled = false
end if
End Sub



When using this code, it still goes from text1 to text3, although I specifically told him to go to text2

Any comments/suggestions?

Tom Cannaerts
slisse@planetinternet.be

The best way to escape a problem, is to solve it.

Clearcode
March 27th, 2001, 04:19 AM
The problem is that the Validate() event occurs after the textbox control has notified the parent form that it has lost the focus.

At this point the control then decides where to put the focus and then calls the Validate event().

The cludge fix for this is to have a picturebox control with tabindex one more than the listbox and in its gotfocus event:

public Sub picDummy_GotFocus()

If text1.text <> "" then
text2.enabled = true
text2.setfocus
else
text2.enabled = false
If text3.Enabled And text3.Visible then
text3.SetFocus
End If
end if

End Sub




HTH,
Duncan


-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com

coolbiz
March 27th, 2001, 08:48 AM
I think using the LostFocus() event is better than using the Validate() event in this case.

Validate() is sent when right after the LostFocus() of Text1 but right before GetFocus of Text3. Therefore when you change the focus to Text2, it actually goes there for few nanoseconds, but because of the GetFocus event is queued for Text3, then the focus goes back to Text3 instead of staying on Text2 (this is from what I understand anyway).

Validate event is also meant when you want to validate the data of previous control (Text1) when it is about to enter the next control (Text2). If data of Text1 is invalid, then you'll set the Cancel = True which will bring back the focus to Text1 instead of going to Text3.

-Cool Bizs