Click to See Complete Forum and Search --> : how to get an answer from a modal dialog


klasg_
January 25th, 2000, 08:18 AM
I want to detekt if OK or some other button was hit to close a modal dialog.

Aaron Young
January 25th, 2000, 08:35 AM
If this Modal Dialog is one of your own Forms, then When the Buttons Are Clicked, Store a Value in the Forms Tag Property to indicate which button was pressed, then Hide the Form instead of Unloading it. This will return you to your Calling Forms Code and you can retrieve the Button Value from the Hidden Forms Tag Property, then Unload it, ie.

private Sub Command1_Click()
Form2.Show vbModal
If Form2.Tag = 1 then
MsgBox "You Pressed Close"
End If
Unload Form2
End Sub



In Form2..

private Sub cmdClose_Click()
Tag = 1
Hide
End Sub



If you were refering to a Messagebox, compare the return value to the VB Constants, ie.
vbYes, vbNo, etc..

Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com

Chris Eastwood
January 25th, 2000, 09:07 AM
I treat everything (well, nearly everything) as a class module in VB (at least, since v4).

A form can have public / friend / private properties and methods just like a class module, which makes it ideal for this kind of work.

Supposing you had a form (frmMyForm) with just two buttons (cmdOK and cmdCancel) - you wanted to display this form and then determine what button the user pressed. You could have the following code in the frmMyForm :


option Explicit
'
private mbCancelled as Boolean
'
public property get Cancelled() as Boolean
Cancelled = mbCancelled
End property
'
private Sub cmdCancel_Click()
mbCancelled = true
me.Hide
End Sub
'
private Sub cmdOk_Click()
mbCancelled = false
me.Hide
End Sub




Now in your main form, when you want to show this modal form, you'd use code such as :


Dim fTemp as frmMyForm
'
' Create a new instance of frmMyForm and show it modally.
'
set fTemp = new frmMyForm
Load fTemp
'
fTemp.Show vbModal
'
' At this point the form is still in memory as we hold a reference to it
'
' - get the value from the public 'Cancelled' property
'
If fTemp.Cancelled = true then
MsgBox "Cancel pressed"
else
MsgBox "OK pressed"
End If
'
Unload fTemp
set fTemp = nothing




- of course this is just a simlple example, you'd probably have other properties on the form to get any values from textboxes / listboxes / combo's etc.


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb