|
-
Nov 14th, 2002, 05:14 PM
#1
Thread Starter
Lively Member
How to use a modal form as an all purpose messagebox...
Hello. I ask a lot of questions.
I have a VB6 program within which I'd like to use one form as a default message box (for design reasons, the people I'm working with do not want to use the default windows MsgBox form. So, I designed a modal form with a yes and no button on which the label changes. Right now I have hidden text boxes all over the place that register which button the user pressed, so when a button is pressed a "Y" or "N" is put into this hidden box and if then logic determines what to do based upon the what is in the hidden text box (whew... that sentence was too long...). Right now it works, but it's sloppy. When the form is invoked, it has to load all of the forms in which text boxes will be placed, so the app is slower and bulkier than it should be. Should I be passing a function to the form? Is there a way to do this so I don't have to use all of these hidden text boxes and keep the form as an all purpose custom messagebox?
Thank you,
Ed
-
Nov 14th, 2002, 05:20 PM
#2
Create a property for the form as type vbMessageResult (I think... it's something like that -- can't get to VB at the moment to check).
Something like this:
VB Code:
Private mlngResult as vbMessageResult
public Property Get Result() As vbMessageResult
Result = mlngResult
End Property
Private Sub cmdYes_Click()
mlngResult = vbYes
Me.Hide
End Sub
Private Sub cmdNo_Click()
mlngResult = vbNo
Me.Hide
End Sub
Then call the form like so:
VB Code:
Load frmDialog
frmDialog.Show vbModal
If frmDialog.Result = vbYes Then
MsgBox "Yes Clicked!"
Else
MsgBox "No Clicked"
End IF
-
Nov 14th, 2002, 05:26 PM
#3
Addicted Member
Here is my custom Message Box function. It returns the response.
This is the Function
VB Code:
Public Function DoMessage(msgType As Long, msgText As String, msgButtons As Long, msgTitle As String) As Long
Dim l As Long
Dim w As Long
Dim h As Long
Dim cw As Long
Dim msgCap As String
msgReply = 0
msgCap = "My Message Box"
If msgTitle <> "" Then
msgCap = msgCap & " - " & msgTitle
End If
'msgType
'101 = Stop - critical
'102 = Question
'103 = Excalmation
'104 = Information
'msgText = text to display
'msgButtons
'0 = Ok Only
'1 = OK and Cancel
'2 = Yes and No
'DoMessage returns the answer
'1 = OK button
'2 = Cancel button
'6 = Yes button
'7 = No button
With dlgMsg
.Caption = msgCap
.picMsg.Picture = LoadResPicture(msgType, 1)
.lblMsg.Caption = msgText
'set the dialog width
l = .lblMsg.Left + .lblMsg.width
If l >= dlgMsg.width Then
dlgMsg.width = l * 1.05
End If
'set the dialog height
h = .lblMsg.Top + .lblMsg.Height
If h >= .cmdOk.Top Then
dlgMsg.Height = dlgMsg.Height + (h - .cmdOk.Top) * 1.15
.cmdOk.Top = dlgMsg.Height - 852
.cmdCan.Top = .cmdOk.Top
End If
'center the two command buttons
w = dlgMsg.width
cw = .cmdOk.width + .cmdCan.width + 105
.cmdOk.Left = (w - cw) / 2
.cmdCan.Left = .cmdOk.Left + .cmdOk.width + 105
Select Case msgButtons
Case 0
.cmdOk.Caption = LoadResString(lV + 150)
.cmdOk.Tag = "OK"
.cmdCan.Visible = False
'center the OK button
w = dlgMsg.width
cw = .cmdOk.width
.cmdOk.Left = (w - cw) / 2
Case 1
.cmdOk.Caption = LoadResString(lV + 150)
.cmdCan.Caption = LoadResString(lV + 151)
.cmdOk.Tag = "OK"
.cmdCan.Tag = "Cancel"
Case 2
.cmdOk.Caption = LoadResString(lV + 159)
.cmdCan.Caption = LoadResString(lV + 160)
.cmdOk.Tag = "Yes"
.cmdCan.Tag = "No"
End Select
End With
dlgMsg.Show vbModal
DoMessage = msgReply
End Function
This is a calling example from a standard error condition
VB Code:
DoMessage 101, Err.Number & ": " & Err.Description, 0, ""
Obviously I am using Resource data as this is a multilingual application. However, look at the function carefully, and you should get the idea.
I see that TechGnome has also provided a simpler approach.
Some days you're the dog,
and some days you're the hydrant.
VB6 Enterprise
-
Nov 14th, 2002, 05:34 PM
#4
Thread Starter
Lively Member
Man - I can't believe this group - it's too good to be true. I post a message and within a half hour I have 2 cool answers both of which work. I also found (by searching the forum, which I should've done before posting, sorry) a third option which is similar to the first response:
'In your calling form
frmForm.Show vbModal, Me
If frmForm.OK Then
MsgBox "OK Pressed"
End If
Unload frmForm
'In the dialog form
Private mblnOK As Boolean
Public Property Get OK() as Boolean
OK = mblnOK
End Property
Private Sub cmdOK_Click()
mblnOK = True
Me.Hide
End Sub
I tested this method and it's working - I also like the vbMessageResponse method - that seems like it might be cleaner.
Thanks everyone, this is a great board!
Ed
-
Nov 14th, 2002, 05:36 PM
#5
I like your approach PhilRob.... just out of curiosity, have you thought about converting msgType into an enumerated data type? Same w/ msgButtons.....
Also did you know this works?
VB Code:
Public Enum enumErrorNumbers
myOK = vbOk
myCancel = vbCancel
.... etc ....
End Enum
-
Nov 15th, 2002, 08:47 AM
#6
Addicted Member
To TechGnome
Actually, I did not even consider a custom type when I did this. I had a problem with languages and had to get around the Standard Message box coming up in Swahili when the app was being run in English on another language version of Windows.
I think it is well worth considering for a future release. Thanks for the idea!
So many good ideas......
Some days you're the dog,
and some days you're the hydrant.
VB6 Enterprise
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|