Results 1 to 6 of 6

Thread: How to use a modal form as an all purpose messagebox...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    88

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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:
    1. Private mlngResult as vbMessageResult
    2.  
    3. public Property Get Result() As vbMessageResult
    4.   Result = mlngResult
    5. End Property
    6.  
    7. Private Sub cmdYes_Click()
    8.   mlngResult = vbYes
    9.   Me.Hide
    10. End Sub
    11.  
    12. Private Sub cmdNo_Click()
    13.   mlngResult = vbNo
    14.   Me.Hide
    15. End Sub

    Then call the form like so:
    VB Code:
    1. Load frmDialog
    2. frmDialog.Show vbModal
    3. If frmDialog.Result = vbYes Then
    4.   MsgBox "Yes Clicked!"
    5. Else
    6.   MsgBox "No Clicked"
    7. End IF
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Addicted Member PhilRob56's Avatar
    Join Date
    Oct 2002
    Location
    New York
    Posts
    249
    Here is my custom Message Box function. It returns the response.

    This is the Function

    VB Code:
    1. Public Function DoMessage(msgType As Long, msgText As String, msgButtons As Long, msgTitle As String) As Long
    2.    
    3.     Dim l         As Long
    4.     Dim w         As Long
    5.     Dim h         As Long
    6.     Dim cw        As Long
    7.     Dim msgCap    As String
    8.    
    9.     msgReply = 0
    10.     msgCap = "My Message Box"
    11.    
    12.     If msgTitle <> "" Then
    13.         msgCap = msgCap & " - " & msgTitle
    14.     End If
    15.  
    16.    
    17.     'msgType
    18.     '101 = Stop - critical
    19.     '102 = Question
    20.     '103 = Excalmation
    21.     '104 = Information
    22.    
    23.     'msgText = text to display
    24.    
    25.     'msgButtons
    26.     '0 = Ok Only
    27.     '1 = OK and Cancel
    28.     '2 = Yes and No
    29.    
    30.     'DoMessage returns the answer
    31.     '1 = OK button
    32.     '2 = Cancel button
    33.     '6 = Yes button
    34.     '7 = No button
    35.    
    36.     With dlgMsg
    37.         .Caption = msgCap
    38.         .picMsg.Picture = LoadResPicture(msgType, 1)
    39.         .lblMsg.Caption = msgText
    40.        
    41.         'set the dialog width
    42.         l = .lblMsg.Left + .lblMsg.width
    43.         If l >= dlgMsg.width Then
    44.             dlgMsg.width = l * 1.05
    45.         End If
    46.        
    47.         'set the dialog height
    48.         h = .lblMsg.Top + .lblMsg.Height
    49.         If h >= .cmdOk.Top Then
    50.             dlgMsg.Height = dlgMsg.Height + (h - .cmdOk.Top) * 1.15
    51.             .cmdOk.Top = dlgMsg.Height - 852
    52.             .cmdCan.Top = .cmdOk.Top
    53.         End If
    54.        
    55.         'center the two command buttons
    56.         w = dlgMsg.width
    57.         cw = .cmdOk.width + .cmdCan.width + 105
    58.         .cmdOk.Left = (w - cw) / 2
    59.         .cmdCan.Left = .cmdOk.Left + .cmdOk.width + 105
    60.        
    61.         Select Case msgButtons
    62.             Case 0
    63.                 .cmdOk.Caption = LoadResString(lV + 150)
    64.                 .cmdOk.Tag = "OK"
    65.                 .cmdCan.Visible = False
    66.                
    67.                 'center the OK button
    68.                 w = dlgMsg.width
    69.                 cw = .cmdOk.width
    70.                 .cmdOk.Left = (w - cw) / 2
    71.                
    72.             Case 1
    73.                 .cmdOk.Caption = LoadResString(lV + 150)
    74.                 .cmdCan.Caption = LoadResString(lV + 151)
    75.                 .cmdOk.Tag = "OK"
    76.                 .cmdCan.Tag = "Cancel"
    77.                
    78.             Case 2
    79.                 .cmdOk.Caption = LoadResString(lV + 159)
    80.                 .cmdCan.Caption = LoadResString(lV + 160)
    81.                 .cmdOk.Tag = "Yes"
    82.                 .cmdCan.Tag = "No"
    83.         End Select
    84.     End With
    85.     dlgMsg.Show vbModal
    86.     DoMessage = msgReply
    87. End Function

    This is a calling example from a standard error condition

    VB Code:
    1. 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

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2002
    Posts
    88
    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

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    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:
    1. Public Enum enumErrorNumbers
    2.   myOK = vbOk
    3.   myCancel = vbCancel
    4. .... etc ....
    5. End Enum
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Addicted Member PhilRob56's Avatar
    Join Date
    Oct 2002
    Location
    New York
    Posts
    249

    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
  •  



Click Here to Expand Forum to Full Width