Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send focus back to previous control 2

Status
Not open for further replies.

ChrisN

Programmer
Jul 9, 2001
59
0
0
GB
Hi

If I run a procedure on a text boxes contents when the text box loses focus, how can I return to that control if an error criteria has been met?

TIA,
Chris
 
Try this
Code:
Text1.SetFocus
Text1.SelStart = 1
Text1.SelLength = Len(Text1.Text)
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Try this
Code:
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Sorry, I should have made myself a bit clearer.

In this situation, the same procedure is called by two text boxes when each loses the focus, therefore I would need to return to a specific control dependant upon the one which had just lost the focus.

Not sure if I am being very clear here! Hope you understand.

Cheers,
Chris
 
I assume you call the procedure say <doSomething> from the lostfocus event of each text box?

pass an extra parameter in
<doSomething>(nBoxFlag as integer)
and add
Code:
select case nBoxFlag
case 1
 txtBox1.SetFocus
case 2
 txtBox2.setfocus
end select
to the end of the <doSomething> procedure.

Hope that helps

Matt
 
Use the validate event of the text control. It is described in the MSDN help. Before a control loses focus the validate event will fire. In here you can do your checking. You then set the &quot;keepfocus&quot; boolean to determine whether or not to return focus to the text control or set focus to the next control.

Keith
 
Here's one basic method. The example requires a form with two text boxes and a command button:
[tt]
Option Explicit

Private Declare Function GetFocus Lib &quot;user32&quot; () As Long
Private Declare Function apiSetFocus Lib &quot;user32&quot; Alias &quot;SetFocus&quot; (ByVal hwnd As Long) As Long

Private LastRecordedFocus As Long

Private Sub Command1_Click()
apiSetFocus LastRecordedFocus
End Sub

Private Sub Text1_LostFocus()
LastRecordedFocus = Text1.hwnd
End Sub

Private Sub Text2_LostFocus()
LastRecordedFocus = Text2.hwnd
End Sub
 
Try this

Code:
Private Sub sf(tb As TextBox)
tb.SetFocus
tb.SelStart = 0
tb.SelLength = Len(Text1.Text)

End Sub

Private Sub Text1_LostFocus()
Call sf(Text1)
End Sub

Private Sub Text2_LostFocus()
Call sf(Text2)

End Sub

You'll need to make sure that you do something in the sub to prevent 'bounce' between the 2 textboxes if they are both enabled, and you can tab from 1 to the other, but this should get you started Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Using the lost focus to perform error checking on multiple controls can have some unforseen disadvantages in this situation. Suppose that both TextBoxes require input. If textbox1 loses the focus to textbox2, and textbox1 contains an error, then you will try to return to textbox1. That in turn causes textbox2 to lose focus, but its error handler also fails, so you attempt to return to textbox2, which again triggers the lost focus of textbox1.

I think you'd be much better off if you called your error handler from the Validate event, rather than the LostFocus event. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top