Results 1 to 6 of 6

Thread: detecting which button the user selected?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,091
    Hi,

    I have a form with 2 peices of code on it. When the user presses the OK button, a form pops up which acts like a message box (but it isn't.. I couldn't use the built in MsgBox for a reason, so don't suggest using the MsgBox) which has 2 buttons on it.. OK and Cancel. What I need to do is when the user clicks on a button, the form closes and returns to the original form and runs the peice of code depending on which button the user selected in the other form.

    So the question is, how do I pass back to the original form the button that the user clicked on?

    Thanks,

    Dan


  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    I would suggest something like this:

    Public clickedButton as String

    <-- Form1 -->

    Private sub cmdOK_Click()
    otherFormName.Show
    End Sub

    Public sub getButton()
    If (clickedButton = "OK") Then
    ...
    ...
    Elseif (clickButton = "Cancel") Then
    ...
    ...
    End If
    End Sub

    <-- Other Form -->

    Private sub cmdOK_Click()
    form1.clickedButton = "OK"
    form1.getButton
    Unload Me
    End Sub

    Private sub cmdCancel_Click()
    form1.clickedButton = "Cancel"
    form1.getButton
    Unload Me
    End Sub

    but there's probably a simpler way.

  3. #3
    Member
    Join Date
    Sep 2000
    Location
    The Netherlands
    Posts
    42
    Hello Dan,

    What you might give a go is to declare to public Boolean variables in a module. One named bOKClicked and one bCancelClicked.
    When to OK-button on the first form is pressed, you set both variables to False. Then you load the MsgBox-like form.
    In the OK-button click event of the MsgBox-like form you set the bOKClicked variable to True and in the Cancel-button click event you set bCancelClicked to True. Then unload that MsgBox-like form.
    Now you should return to the OK-button click event on the first form. Now you have to simply check which variable is set to True and depending on it run the rest of your code.

    A code example might look like this:
    (in the example, I called the first form frmMain and the second on frmConfirm)

    frmMain OK-button Click event:
    Code:
    Private Sub OK_Click()
    ' Set both variables to false
    bOKClicked = False
    bCancelClicked = False
    
    ' Load the confirmation form
    frmConfirm.Show vbModal
    
    ' Declare label to return to the check
    CheckAnswer:
    ' Here we return after the frmConfirm is unloaded, so now
    ' check the values of the variables...
    If bOKClicked Then
        ' bOKClicked = True
        ProcessOption ' Call a subroutine to execute code
    ElseIf bCancelClicked Then
        ' bCancelClicked = True
        QuitProgram ' Call a sub to end the program
    Else
        ' Neither variables are True
        frmConfirm.Show vbModal ' Load the second form again
        Goto CheckAnswer        ' Check the values again
    End If
    
    End Sub
    frmConfirm OK-button Click event:
    Code:
    Private Sub OK_Click()
    bOKClicked = True
    Unload frmConfirm
    End Sub
    frmConfirm Cancel-button Click event:
    Code:
    Private Sub Cancel_Click()
    bCancelClicked = True
    Unload frmConfirm
    End Sub
    I hope this helps you.

    Greetings
    Greetings,

    Hajo Dijkstra


  4. #4
    Lively Member
    Join Date
    Feb 2000
    Posts
    118

    Huh

    if you have two pieces of code on the first form, and you want the MsgBox like form to set which piece of code is run, why dont you just put the code in the click event of the MsgBox form instead of the first form.
    Kokopeli
    VB6 SP3

  5. #5
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    More answers

    You no doubt have more answers than you need by now, so a
    couple more will not hurt right?

    1) If the message form you use is multi-putpose (i.e. you
    use it alot in your code) then you can certainly do as
    others suggest and have it store the last button pressed in
    a module level variable in the form.

    In this case, you would NOT call unload on the form for the
    duration of your application. Just show and hide it. You
    would put your own code in query_unload event to prevent
    the form from unloading via the x (or alternatively do not
    give the user the close control)

    Then from the calling form, directly after the call
    MyMsgBox.show vbModal, check the value of the variable,
    e.g. theChoice = MyMsgBox.Choice.

    2) If for your own reasons you want the message box to be
    unlaoded after it is used, then create a method in the
    calling form (and any other form it could be called from)
    to allow the message box form to set a variable. This is
    not a good solution in OO terms so I would suggest you
    avoid it. Ideally, you do not want the child form to be
    messing around with the parent ...


    On a final note, I seldom use the MsgBox except for
    debugging as it is not at all very "pretty" I prefer to
    use a custom message box which can have my company logo,
    maybe an animated icon, the message anf some buttons of
    course, and maybe even a progress bar if appropriate.


    So good luck with the suggestions made. Try to make your
    message box replacement as generic as possible, then you
    will find that you can reuse it untold times

    Regards
    Paul Lewis

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    'as many ways as programmers
    'boolean value and case select

    Code:
    'bas module code
    Option Explicit
    
    Public Yes As Boolean
    
    'Main Form Code
    
    Option Explicit
    
    ' need this as you don't want
    ' to active the code in form activate
    ' on first opening of forms
    
    Dim myCount As Integer
                             
    Private Sub Command1_Click()
    'open your pretent message box (form2)
        Form2.Show
        Form1.Hide
    End Sub
    
    Private Sub Form_Activate()
    'if not the first time opening use case else
    'bypass and increment myCount
    'on 2nd time through react to either yes or no
    
        If myCount > 0 Then
            Select Case Yes
               Case True
                  MsgBox "yes" 'your code
               Case False
                   MsgBox "no" 'your code
            End Select
        End If
            myCount = myCount + 1
    End Sub
    '''''''''''''''''''''''''''''''''''''''''''''''''''
    'Form2 (message form) code
    
    Option Explicit
    
    Private Sub Command1_Click()
    'value yes and return
        Yes = True
        Form1.Show
        Form2.Hide
    End Sub
    
    Private Sub Command2_Click()
    'value yes and return
        Yes = False
        Form1.Show
        Form2.Hide
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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