dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Form return values

  1. #1
    Join Date
    Nov 2003
    Posts
    12

    Question Form return values

    I'd like to build up a form with 'ok' and 'exit' button, returning vbOk or vbExit respectively when I unload it, as I do with msgbox function. How can I do it?
    Thanks, Faggio

  2. #2
    Join Date
    Apr 2005
    Location
    India
    Posts
    271

    Smile Re: Form return values

    Dear faggio,

    Follow these steps

    Step 1 :

    Add 2 forms to yuor project named Form1 and f

    f is the form that we are going to use like msgbox

    Step 2 :

    in f add 2 buttons 'cmdCancel' and 'cmdOk'

    Step 3 :

    in the declaration section of the code window of form 'f' add a declaration like the below given one

    Code:
    Public r As String
    Step 4 :

    Add the below code also to form 'f'

    Code:
    Private Sub cmdCancel_Click()
    r = "Cancel"
    Unload Me
    End Sub
    
    Private Sub cmdOk_Click()
    r = "Ok"
    Unload Me
    End Sub
    Step 5 :

    Now in the form1 we are going to call the form 'f'

    add a button command1 to form1 and place the below code


    Code:
    Private Sub Command1_Click()
    Dim ff As New f
    ff.Show vbModal
    MsgBox ff.r
    End Sub
    That's it

    Pramod S Nair
    Learn by Sharing.

    You can use some free things i have done from www.wisdombay.com

  3. #3
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: Form return values

    I personally like to use a slightly different method that avoids global variables. You can add properties to the form for it's return values (or to pass information to it when you show it), and then instantiate it like any other class. You can even add custom methods to the form, just remember that you can only call properties or methods before or after a modal form is shown. For an example of how this works, create a form called Form1 with cmdOK and cmdCancel buttons with the following code:
    Code:
    Option Explicit
    
    Private ciReturn As Integer
    
    Public Property Get ReturnValue() As Integer
    
        ReturnValue = ciReturn
        
    End Property
    
    Private Sub cmdCancel_Click()
    
        ciReturn = 0
        Me.Hide
    
    End Sub
    
    Private Sub cmdOK_Click()
    
        ciReturn = 1
        Me.Hide
    
    End Sub
    Call it like this:
    Code:
    Private Sub Example()
    
        Dim oQuery As Form1, iReturn As Integer
        
        Set oQuery = New Form1                  'Create a new instance of the form.
        oQuery.Caption = "Select OK or Cancel"  'Set it's caption (or other properties).
        oQuery.Show vbModal                     'Show it and wait for it to complete.
        
        iReturn = oQuery.ReturnValue            'Get the return value from the form.
        Debug.Print iReturn
        
        Unload oQuery                           'Unload it.
        Set oQuery = Nothing                    'Release the reference.
    
    End Sub

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Form return values

    Here is another simple method.

    Say Form2 is the form that you want to use as messagebox. Put two command buttons on the Form named cmdOk and cmdCancel. Now copy this code
    Code:
    Private okPressed As Boolean
    Public Function ShowMe() As Integer
        Me.Show vbModal
        If okPressed Then
            ShowMe = vbOK
        Else
            ShowMe = vbCancel
        End If
    End Function
    
    Private Sub cmdOk_Click()
        okPressed = True
        Unload Me
    End Sub
    
    Private Sub cmdCancel_Click()
        okPressed = False
        Unload Me
    End Sub
    Now whenever you want to call the form, you can use
    Code:
        Dim myMessage As Form2
        Set myMessage = New Form2
        If myMessage.ShowMe = vbOK Then
            MsgBox "Ok Was presed"
        Else
            MsgBox "Cancel Was presed"
        End If
        Set myMessage = Nothing
    Remember Form2 will always be shown as Modal.

  5. #5
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Re: Form return values

    If you don't want to use an extra variable to store the value there is a Tag property that you can use.
    Busy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)