Textbox Killfocus event.

Started by Petrus Vorster, July 11, 2015, 04:22:28 PM

Previous topic - Next topic

Petrus Vorster

I have noticed a peculiar occurrence in one of my routines using the Killfocus event in a textbox.

One this event happens, it runs another function to check validity of the info entered.
In that function i just placed a msgbox for me to see in the interim if the information it reads from the file is correct or not.

There is only ONE time in that function where i call the MSGBOX, but it pops up twice as if the Kill-focus event is happening twice.
I will eventually remove the MSGBOX, but I am wondering why this will be happening as there is but only two lines of code in there, so I cant be lost in a pile of code to miss anything.

I am just curious because i will place more routines in there and it will be weird if it actually runs twice.
-Regards
Peter

Petrus Vorster

-Regards
Peter

David Kenny

At first I didn't see how this would happen as only one control in the array will have focus and therefor only one can lose that focus. 

But you are using a messagebox for your output.  So, control1 loses focus to control2, firing the the killfocus event.  Your routine then gets called and calls the messagebox which steals focus from control2, firing the killfocus event again.

Have you tried ztrace (by Patrice Terrier, with FF support) yet?  You can write to it many times and will only steal focus from your program once each time it has to create it's window. You can leave the ztrace window up, or close it whenever you want to.  Using it is as easy as enabling it in your FF project, and then adding a call wherever you need it. Something like: ztrace "Lost focus: Data is " & MyValidityCheck(HWND_Form1_Txtbox(1))
ztrace "Any valid string"
ztrace "Loop Count:" & str$(i)

Petrus Vorster

Hi David

I felt so stupid when i realized the killfocus got passed on to every other Textbox in the array due to the MSG Box.
Its like a killfocus fired for all of them at once.

I fixed that quite easily by just using a Case like this:
Select Case controlindex
Case 0
Myproduct& = FF_ComboBox_GetCurSel(HWND_RENTALS_COMBO2)
'0 = Box
'1 = Bag
If myproduct& = 0 Then
iscorrectbox& = validateboxnum
End If
If iscorrectbox& = 1 Then
      openBOX
End If

'
'sit kode later hier in vir privaatsak validate
'///PARDON THE OTHER LANGUAGE GUYS! /////

Case Else
Exit Function


BUT:
I now have a MSGBOX that the user must respond to and when he closes the program unexpectedly, the event still runs first.
This leaves you with a program that is no longer on screen, but the warning MSGBox still proudly sits on screen.
How do I prevent this killfocus to run at all when the user decides to terminate the program???

Its something i will need to catch somehere in the Message Pump I think, but I have no idea how to do that.
Any suggestions?
-Regards
Peter

David Kenny

Peter,

QuoteIts like a killfocus fired for all of them at once.
The killfocus didn't get passed on to every other Textbox in the array. I gave you the explanation above about why you are getting two killfocus events.  The fact that you have two controls in that array is just a coincidence.

A better way to deal with the messagebox problem might be to create your own messagebox substitute.  The standard messagebox is very quick and dirty and is suitable for many programs, but you will have much more control over the behavior of your substitute window.

Petrus Vorster

#5
Hi David

The first issue was resolved. I understand what you said. I just didnt explain so well...
Thanks for the advice.

I like to make trouble for myself it seems...
Now, when the Killfocus happens, everything goes well.
The tool checks whether the user entered a valid Postbox sequence for a specific branch. (They vary from town to town).
If not, it gives a messagebox (the FF version) that the box number is incorrect for that branch, delete the entered number and reset the focus back to that textbox.
All well.
UNLESS the user terminates the program while an invalid box number was entered. Then the program terminates, but my warning box still pops up.
That I want to prevent.
I added two images of the correct and incorrect occurrences below:
I need to prevent that action when the program closes, otherwise it works correctly. Hope Its better explained.
-Regards
Peter