Home All Groups Group Topic Archive Search About

Modal form stops code in another form

Author
19 Oct 2005 8:47 PM
Dave
I have code running in form 1 in a loop.  The loop calls DoEvents frequently
so the UI is responsive.  When I bring up a modal dialog by pressing a button
on form 1, the code on form 1 stops running until the modal dialog is closed.
Is there a way to keep the code in form 1 running even while the modal form
is up?

Thanks,

Dave

Author
19 Oct 2005 8:54 PM
Karl E. Peterson
Dave wrote:
> I have code running in form 1 in a loop.  The loop calls DoEvents
> frequently so the UI is responsive.  When I bring up a modal dialog
> by pressing a button on form 1, the code on form 1 stops running
>  until the modal dialog is closed. Is there a way to keep the code in
> form 1 running even while the modal form is up?

One possible design would be to convert it to timer-driven, rather than loop-driven,
iterations.
--
Working Without a .NET?
http://classicvb.org/petition
Author
20 Oct 2005 8:23 AM
J French
On Wed, 19 Oct 2005 13:54:31 -0700, "Karl E. Peterson" <k***@mvps.org>
wrote:

>Dave wrote:
>> I have code running in form 1 in a loop.  The loop calls DoEvents
>> frequently so the UI is responsive.  When I bring up a modal dialog
>> by pressing a button on form 1, the code on form 1 stops running
>>  until the modal dialog is closed. Is there a way to keep the code in
>> form 1 running even while the modal form is up?

Why have a Modal Form if you don't want it to behave like one ?
Author
19 Oct 2005 9:02 PM
Someone
First, modal forms behave differently between the IDE and the EXE version. I
know that MsgBox blocks Timer events in the IDE and not in the EXE version.
I don't know about modal forms. Make a simple program that test these...

> Is there a way to keep the code in form 1 running even while the modal
> form
> is up?

Yes. If EXE mode does not behave differently than in IDE mode, then you
could use a second Timer for this. Example:

' Show Form2
Timer2.Enabled = True

Private Sub Timer2_Timer()
    Timer2.Enabled = False
    Form2.Show vbModal
End Sub




Show quoteHide quote
"Dave" <D***@discussions.microsoft.com> wrote in message
news:D497252C-9331-4D06-8866-610F6DB7630C@microsoft.com...
>I have code running in form 1 in a loop.  The loop calls DoEvents
>frequently
> so the UI is responsive.  When I bring up a modal dialog by pressing a
> button
> on form 1, the code on form 1 stops running until the modal dialog is
> closed.
> Is there a way to keep the code in form 1 running even while the modal
> form
> is up?
>
> Thanks,
>
> Dave
>
Author
19 Oct 2005 9:02 PM
Ken Halter
Show quote Hide quote
"Dave" <D***@discussions.microsoft.com> wrote in message
news:D497252C-9331-4D06-8866-610F6DB7630C@microsoft.com...
>I have code running in form 1 in a loop.  The loop calls DoEvents
>frequently
> so the UI is responsive.  When I bring up a modal dialog by pressing a
> button
> on form 1, the code on form 1 stops running until the modal dialog is
> closed.
> Is there a way to keep the code in form 1 running even while the modal
> form
> is up?
>
> Thanks,
>
> Dave

Are you sure you need to show it modal? If so, try the Timer that Karl
mentioned.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
19 Oct 2005 9:03 PM
Jeff Johnson [MVP:VB]
"Dave" <D***@discussions.microsoft.com> wrote in message
news:D497252C-9331-4D06-8866-610F6DB7630C@microsoft.com...

>I have code running in form 1 in a loop.  The loop calls DoEvents
>frequently
> so the UI is responsive.  When I bring up a modal dialog by pressing a
> button
> on form 1, the code on form 1 stops running until the modal dialog is
> closed.
> Is there a way to keep the code in form 1 running even while the modal
> form
> is up?

Yes, but you have to get kind of tricky. First, there's no (simple) way to
pick up RIGHT WHERE YOU LEFT OFF, in other words, if your code looked like
this:

***********************
Private Sub MyProc()
    Dim frmM As frmModal

    x = 1
    x = x + 1
    y = 2
    [...]
    Set frmM = New frmModal
    frmM.Show vbModal

    x = x * 20
************************

you're not going to be able to continue on at the x = x * 20 line without
closing the modal form or calling the procedure again playing with flags and
If statements.

What you CAN do, however, is call ANOTHER procedure in the "main" form from
the modal form. In other words, you could have this in frmModal:

Private Sub Form_Load()
    Form1.RunSomeProcedure
End Sub

Now the modal form will be displayed and the code in the MyProc procedure
will be suspended, but you can still execute code in Form1. Now, you're
probably thinking, "But I set up a whole bunch of local variables in MyProc
and I need to do something with them, and RunSomeProcedure won't know about
them." Yeah, I know. What you're going to have to do is either make all
those variables module-level, create a module-level structure with the
needed info, or create a class which, again, you store in a module-level or
higher-scoped variable. (Well, another option would be to pass all those
variables to the modal form and have it turn around and pass them right back
to Form1, but that's kinda icky in my opinion.)
Author
20 Oct 2005 11:06 AM
Mike Williams
"Dave" <D***@discussions.microsoft.com> wrote in message
news:D497252C-9331-4D06-8866-610F6DB7630C@microsoft.com...

> I have code running in form 1 in a loop.  The loop calls DoEvents
> frequently so the UI is responsive.  When I bring up a modal dialog
> by pressing a button on form 1, the code on form 1 stops running
> until the modal dialog is closed. Is there a way to keep the code in
> form 1 running even while the modal form is up?

I'd agree with Jerry and say "Why have a Modal Form if you don't want it to
behave like one ?". However, if you just want the second Form to be "always
on top of the main Form" while it is being shown (which it appears is what
you actually do want) then simply show it specifying the main Form as its
"owner":

Form2.Show , Me

Mike
Author
20 Oct 2005 1:00 PM
Jeff Johnson [MVP: VB]
Show quote Hide quote
"Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
news:dj7trj$8fr$1@newsg2.svr.pol.co.uk...

>> I have code running in form 1 in a loop.  The loop calls DoEvents
>> frequently so the UI is responsive.  When I bring up a modal dialog
>> by pressing a button on form 1, the code on form 1 stops running
>> until the modal dialog is closed. Is there a way to keep the code in
>> form 1 running even while the modal form is up?
>
> I'd agree with Jerry and say "Why have a Modal Form if you don't want it
> to behave like one ?". However, if you just want the second Form to be
> "always on top of the main Form" while it is being shown (which it appears
> is what you actually do want) then simply show it specifying the main Form
> as its "owner":
>
> Form2.Show , Me

Let's say it's a progress form. During the long-running process you don't
want any input on the main form, so you display the progress form modally.
However, you want the long-running code to be house in the main form, not
the progress form.

Yes, you could simply disable the main form and show the progress form
modelessly but on top, but that's not ideal. For example, If the user clicks
on the app's button in the task bar, the main form will get focus (even
though it's disabled), not the progress form. It doesn't look professional.
Author
20 Oct 2005 1:10 PM
Bob Butler
Show quote Hide quote
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:u0oyMZX1FHA.2792@tk2msftngp13.phx.gbl
> "Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
> news:dj7trj$8fr$1@newsg2.svr.pol.co.uk...
>
>>> I have code running in form 1 in a loop.  The loop calls DoEvents
>>> frequently so the UI is responsive.  When I bring up a modal dialog
>>> by pressing a button on form 1, the code on form 1 stops running
>>> until the modal dialog is closed. Is there a way to keep the code in
>>> form 1 running even while the modal form is up?
>>
>> I'd agree with Jerry and say "Why have a Modal Form if you don't
>> want it to behave like one ?". However, if you just want the second
>> Form to be "always on top of the main Form" while it is being shown
>> (which it appears is what you actually do want) then simply show it
>> specifying the main Form as its "owner":
>>
>> Form2.Show , Me
>
> Let's say it's a progress form. During the long-running process you
> don't want any input on the main form, so you display the progress
> form modally. However, you want the long-running code to be house in
> the main form, not the progress form.

Show the progress bar form modally and have it call back to a procedure in
the main form or use a timer in the main form to kick off the process.  It's
not that the main form can't run code, just that the UI is unresponsive and
the procedure doing the modal show is suspended.

--
Reply to the group so all can participate
VB.Net: "Fool me once..."
Author
20 Oct 2005 1:19 PM
Mike Williams
"Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:u0oyMZX1FHA.2792@tk2msftngp13.phx.gbl...

> Yes, you could simply disable the main form and show the progress
> form modelessly but on top, but that's not ideal.

I agree. But I wasn't sure exactly what the OP wanted (and I'm still not
sure). There are of course lots of different ways of doing most things, and
I'm sure there is an ideal way of doing exactly what the OP wants to do (if
we knew what it was).

> For example, If the user clicks on the app's button
> in the task bar, the main form will get focus (even
> though it's disabled),

But it wouldn't be disabled (unless the OP specifically added code to do
that). He has said that he has DoEvents in his main code loop (that he wants
to keep running) and it might well be that he actually does want the user to
be able to interact fully with both Forms while his code is running and at
the same time for the secondary Form to be always on top of the main Form,
in which case my solution would be fine, with a few subtle amendments to
make it behave in a way that you would approve of, of course :-)

Mike
Author
20 Oct 2005 2:16 PM
Jeff Johnson [MVP: VB]
"Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
news:dj85ga$5rk$1@newsg4.svr.pol.co.uk...

>> Yes, you could simply disable the main form and show the progress
>> form modelessly but on top, but that's not ideal.
>
> I agree. But I wasn't sure exactly what the OP wanted (and I'm still not
> sure).

Now that I look back at the original message I see the comment about the UI
(of the main form??) being responsive and I'm with you: I'm not exactly sure
what he wants. I was commenting on the "how do I make code in Form1 run
while a modal form is displayed" aspect, which I guess is only a piece of
his question.