bizarre VB windowing race condition?

Posted on 2004-04-26
Last Modified: 2010-05-02
ok, I've run up against an irritating problem that seems to be some kind of bug in VB's windowing code.

I have a form (form1) .Show()ing a modal form (form2) which does some stuff. form1 has a control with a LostFocus() event. This event seems to be fired at a peculiar time. When debugging the code (or when using messageboxes in code to indicate order of execution) the lostfocus event fires when the form2.Show() command is given just before form2 is actually shown. Otherwise it seems to fire just after form2 returns. That's the first bug.

The second problem is that I actually don't know what the hell is going on in that latter case (just executing the code without any debugging measures). But for whatever reason it seems to be behaving illogically.

some pseudocode:
  noupdate = true         'noupdate is a private signalling variable inside form1
  form2.Show 1, Me        'modal, Me is owner
  set form1.textbox to form2.returnvar         'returnvar is a public variable set by operations on form2
  noupdate = false
end sub

  if noupdate then exit sub
  set form1.textbox to "0"
end sub

anyone know anything about this or come across similar problems? Other points that might be of interest are that the prog uses functions in a DLL written in C to do some stuff, but none of that happens anywhere near the above code. There are no MDI forms. No windows API code is being used. My DLL doesn't mess about with window procedures or touch the windowing code in any way whatsoever.

I would be especially grateful if anyone can point me to some accurate information about the order of execution of code in these specialised sorts of windowing scenarios in VB.

Thanks in advance.
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

JCinDE earned 125 total points
ID: 10920693
Well I'm not sure what you're trying to accomplish by using the 'lostfocus' event...seems you could just as easily do this:

  some pseudocode:
    set form1.textbox to "0"
    form2.Show 1, Me        'modal, Me is owner
    set form1.textbox to form2.returnvar         'returnvar is a public variable set by operations on form2
  end sub

Can you give us a higher-level understanding of what form behavior you're trying to induce?
ID: 10994399
The main form (form1) is itself simply a dialog for the retrieval of a value from the user. form2 is an optional pop-up that allows you to view/enter/change the value in scientific notation (Ax10^B). The lostfocus event is necessary for input checking - when the textbox loses focus the program assumes that the user is done typing, takes a value from the box and refreshes the box so it displays the value as it has been stored (ie removes any illegal letters, symbols etc).

behavioural flow diagrams:
  [form1_show] -> (set textbox to VALUE)
  [textbox_keypress_vbCR] -> (if len(textbox) then VALUE = val(textbox)),(textbox = str(VALUE))
  [textbox_lostfocus] -> Same as above
  [textbox_dblclick] -> (show form2 modally)
*[form2_show] -> (set TEMPVALUE to VALUE), (set form2 textboxes to values based on TEMPVALUE)
  [form2_textboxes_lostfocus or vbCR] -> (TEMPVALUE = textbox values etc, like above)
  [form2_ok_button] -> (VALUE = TEMPVALUE), (hide), (refresh form1)
  [form2_cancel] -> (just hide form)

*it's around here that things get wierd. If debugging, lostfocus is called, and then execution carries on as normal. If running normally, then the lostfocus event is somehow queued here, but doesn't fire until form 2 returns (these are modal forms remember), so after the changes etc, form1_textbox_lostfocus fires and resets the value we got back from form2. This is probably all a result of the way in which VB's runtimes create these 'modal' forms, which AFAIK is not a builtin feature of the windowing engine.

I'm presently trying to figure out other ways of doing what I want to do (it's currently looking like a timer will be necessary to 'schedule' things properly) so the problem isn't of the 'please help me do XYZ' variety, more a 'what the hell is going on, anyone know?'.

Thanks for the suggestion JCinDE, but no cigar just yet! I'll keep digging to see if I can find anything, I'll report back if I make any progress. Any time you can spend on this is greatly appreciated.

ID: 11258291
dammit, why can no-one ever answer any of my questions? give the points to JCinDE. I guess he tried.

ID: 11425904

Alas, it was rhetorical, but I thank you for your suggestions. I apologise if I seem ungrateful for the services provided by you and your cohorts, I do not mean to cause offence.

The reason for the comment is my frustration at seeming to have developed an ability to come up against really obscure problems such as the one above. With regard to your point 3 above, I generally do not especially want people to test my problems out. I will usually bang my head against a problem for a day or two before bringing it here, so I'm usually fairly certain of the parameters, and I'm more in search of advice from someone who has experienced the problem themselves and as such has expertise. It is advice and insight I seek, not man hours. However I cannot complain if that is what people have to offer here. I guess the motivation of points (and thus the selectiveness of attention) is to be expected as well, after all it is the currency of this exchange. However this brings me on to your point 4 - professionalism. My response to that would be that I am *glad* I sound unprofessional. I do this for love not money! I feel that keeping one eye on the money is not information technology - it's business, and I don't feel that business is always particularly good for IT. Personally I will make suggestions on any question I feel competent in answering, regardless of points, but again, it's your ball so I guess you get to choose the game :)

With regard to point 2, yes I agree the name is pretty offensive. Originally it was just a shameless website plug, and the account's deactivation prompted me to start a new account with a slightly moronic 'retort'. I keep forgetting that the way to get things changed around here is to make requests in CS - I had just assumed that the lack of 'change username' meant it couldn't be done. I'll probably tap something out to CS and try to get it changed. Failing that I'll just shut down the account and start again. Incidentally, if long names are so offensive why does EE allow people such things?

It's a shame my 'voice' comes across as expressing a (bad) attitude, it certainly isn't meant as such. I fear that people may take things more seriously than I, and perhaps I should steel myself for more misunderstandings in the future. I hope however that you're able to see that I'm keen to learn and share my knowledge and I trust that keeps me well within the template of a welcome user of this exchange.


Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question