?
Solved

bizarre VB windowing race condition?

Posted on 2004-04-26
7
Medium Priority
?
225 Views
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:
form1_textbox_doubleclick()
  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

form1_textbox_lostfocus()
  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.
0
Comment
[X]
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
7 Comments
 
LVL 5

Accepted Solution

by:
JCinDE earned 500 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:
  form1_textbox_doubleclick()
    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?
0
 
LVL 2
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)
  [etc]
  [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.

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

=me=
0
 
LVL 2
ID: 11425904
Dan,

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.

=me=
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

752 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