Getting a return value from a modal dialog

J

John

Hi

I have a OK/Cancel modal dialog/form. Is there a way to get a return value
from the dialog to know if user pressed either Ok or cancel button?

Thanks

Regards
 
M

Minton M

Hi

I have a OK/Cancel modal dialog/form. Is there a way to get a return value
from the dialog to know if user pressed either Ok or cancel button?

Thanks

Regards

The simple way to do this is to create an event for your form - it
makes it reusable too. In modal dialog:

Raise Event Some_Event(sSomeValue as string)

Public Function Button_Click() (or whatever handler)
 
M

Minton M

The simple way to do this is to create an event for your form - it
makes it reusable too. In modal dialog:

Raise Event Some_Event(sSomeValue as string)

Public Function Button_Click() (or whatever handler)

Sorry, I hit send too soon...

Do a 'dim withevents' from the calling form and capture the value back
that way. I can provide more code if this isn't clear but it works
*beautifully* and helps you make forms that can be reused without
tweaking. I use it all the time for kiosk applications. It's
especially neat because you can publish the event to more than one
consuming object without your form dialog having to know about it.

Anyway, hope this helps,
James
 
M

Minton M

Hi Minton

A code example would be great.

Thanks

Regards

Ok, this is untested and pre-caffeine but should work just fine:

In the modal dialog, in the general declarations:

Event FetchedValue(lValue as long, bCanceled as boolean)

Private sub OK_Click()
Raiseevent FetchedValue(1,False)
End Sub

Private sub Cancel_Click()
Raiseevent FetchedValue(2,True)
End Sub

Then in the calling form:

(General decs)
Dim WithEvents fMyDialog as Form_frmModalDialog

Public Function fMyDialog_FetchedValue(lValue as long, bCanceled as
boolean) -- NOTE: this is found from the procedure dropdowns and
has to be selected this way
if not bcanceled then
msgbox "I just got " & lValue & " from the dialog."
end if
end function

The nice thing about this is that you don't need to halt code (useful
for when timers are running, for example) and if you pass the
fMyDialog reference around, other objects can subscribe to the events
too. That's obviously not so useful for an OK/cancel dialog, but works
great for forms such as keypad entry.

Essentially, this is more of a VB code construct but is extremely
robust. I use this in POS applications for publishing changes in the
order context across all open objects, without having to keep track of
the open objects. Let me know if this helps or if you need more info!

-- James
 
M

Minton M

Ok, this is untested and pre-caffeine but should work just fine:

In the modal dialog, in the general declarations:

Event FetchedValue(lValue as long, bCanceled as boolean)

Private sub OK_Click()
Raiseevent FetchedValue(1,False)
End Sub

Private sub Cancel_Click()
Raiseevent FetchedValue(2,True)
End Sub

Then in the calling form:

(General decs)
Dim WithEvents fMyDialog as Form_frmModalDialog

Public Function fMyDialog_FetchedValue(lValue as long, bCanceled as
boolean) -- NOTE: this is found from the procedure dropdowns and
has to be selected this way
if not bcanceled then
msgbox "I just got " & lValue & " from the dialog."
end if
end function

The nice thing about this is that you don't need to halt code (useful
for when timers are running, for example) and if you pass the
fMyDialog reference around, other objects can subscribe to the events
too. That's obviously not so useful for an OK/cancel dialog, but works
great for forms such as keypad entry.

Essentially, this is more of a VB code construct but is extremely
robust. I use this in POS applications for publishing changes in the
order context across all open objects, without having to keep track of
the open objects. Let me know if this helps or if you need more info!

-- James

Whoops, forgot to mention that you need to create an instance of the
dialog whenever you open it using:

Set fMyDialog = New Form_frmModalDialog
fMyDialog.Visible = TRUE

.... and then destroy it when you're done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top