Results 1 to 7 of 7

Thread: Capturing the value of the button clicked on a msgbox

  1. #1

    Thread Starter
    Lively Member brjames's Avatar
    Join Date
    Jan 2000
    Location
    Tenby, Wales, UK
    Posts
    121

    Capturing the value of the button clicked on a msgbox

    Hi guys

    I need to find out which button has been clicked on a msgbox and then show the user via a text box on my form.

    I have written a program demonstrating the various icon/button combinations for a msgbox. Everything works great. All i need to do now is find out which button on the msgbox the user pressed and display this info in a text box on the form.

    Can anyone help please??

    Ben
    Brjames
    Webmaster and Crewmember
    www.tenbyrnli.co.uk

  2. #2
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    you can do ths:

    VB Code:
    1. Dim Response As String
    2.  
    3. Response = MsgBox("Press Yes or No", vbYesNo)
    4.  
    5. If Response = vbYes Then
    6.     MsgBox "You pressed yes!"
    7. ElseIf Response = vbNo Then
    8.     MsgBox "You pressed No!"
    9. End If

    or a shorter version:

    VB Code:
    1. If MsgBox("Press Yes or No", vbYesNo) = vbYes Then
    2. MsgBox "you pressed yes!" Else MsgBox "you pressed no"
    3. End If
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    The MsgBox function actually returns an integer and not a string, but BuggyProgrammer's code should work. Here is another way to approach the problem:
    VB Code:
    1. Select Case MsgBox("Please click Yes, No or Cancel", _
    2.                        vbYesNoCancel + vbInformation)
    3.         Case vbYes
    4.             MsgBox "You chose Yes"
    5.         Case vbNo
    6.             MsgBox "You chose No"
    7.         Case Else ' Case vbCancel would also work here
    8.             MsgBox "You chose Cancel"
    9.     End Select

  4. #4

    Thread Starter
    Lively Member brjames's Avatar
    Join Date
    Jan 2000
    Location
    Tenby, Wales, UK
    Posts
    121

    Yes that works but. . . . . .

    I need to repeat the same bit of code over and over for each of the buttons on each msgbox that appears. If you look at my code below you will see what I mean.

    Is there any way of typing the code (in the replies above) just once and using it for all of the msgboxes?? Maybe by putting the code above into a module??

    Thanks in advance guys and gals!!!
    Brjames
    Webmaster and Crewmember
    www.tenbyrnli.co.uk

  5. #5

  6. #6

    Thread Starter
    Lively Member brjames's Avatar
    Join Date
    Jan 2000
    Location
    Tenby, Wales, UK
    Posts
    121

    sorry here it is. . . . . . .

    VB Code:
    1. Private Sub cmdShow_Click()
    2. Dim answer As VbMsgBoxResult
    3.  
    4. Dim MsgBoxStyle     As VbMsgBoxStyle
    5.  
    6.     If optButton(0).Value Then
    7.         MsgBoxStyle = vbOKOnly
    8.     ElseIf optButton(1).Value Then
    9.         MsgBoxStyle = vbOKCancel
    10.     ElseIf optButton(2).Value Then
    11.         MsgBoxStyle = vbYesNo
    12.     ElseIf optButton(3).Value Then
    13.         MsgBoxStyle = vbYesNoCancel
    14.     ElseIf optButton(4).Value Then
    15.         MsgBoxStyle = vbRetryCancel
    16.     ElseIf optButton(5).Value Then
    17.         MsgBoxStyle = vbAbortRetryIgnore
    18.     End If
    19.  
    20.     If optIcon(0).Value = True Then
    21.         MsgBoxStyle = MsgBoxStyle
    22.     ElseIf optIcon(1).Value = True Then
    23.         MsgBoxStyle = MsgBoxStyle + vbCritical
    24.     ElseIf optIcon(2).Value = True Then
    25.         MsgBoxStyle = MsgBoxStyle + vbExclamation
    26.     ElseIf optIcon(3).Value = True Then
    27.         MsgBoxStyle = MsgBoxStyle + vbCritical
    28.     ElseIf optIcon(4).Value = True Then
    29.         MsgBoxStyle = MsgBoxStyle + vbInformation
    30.     ElseIf optIcon(5).Value = True Then
    31.         MsgBoxStyle = MsgBoxStyle + vbInformation
    32.     ElseIf optIcon(6).Value = True Then
    33.         MsgBoxStyle = MsgBoxStyle + vbQuestion
    34.     ElseIf optIcon(7).Value = True Then
    35.         MsgBoxStyle = MsgBoxStyle + vbCritical
    36.     ElseIf optIcon(8).Value = True Then
    37.         MsgBoxStyle = MsgBoxStyle + vbExclamation
    38.     End If
    39.  
    40.                
    41.     MsgBox txtMessage.Text, MsgBoxStyle, txtTitle.Text
    42.    
    43. txtResult.Text = answer
    44. End Sub
    Brjames
    Webmaster and Crewmember
    www.tenbyrnli.co.uk

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427
    Do you mean something like this?
    VB Code:
    1. Select Case True
    2.         Case optButton(0).Value
    3.             MsgBoxStyle = vbOKOnly
    4.             txtResult.Text = "OK only"
    5.         Case optButton(1).Value
    6.             MsgBoxStyle = vbOKCancel
    7.             txtResult.Text = "Cancel"
    8.         Case optButton(2).Value
    9.             MsgBoxStyle = vbYesNo
    10.             txtResult.Text = "Yes and No"
    11.         Case optButton(3).Value
    12.             MsgBoxStyle = vbYesNoCancel
    13.             txtResult.Text = "Yes, No and Cancel"
    14.         Case optButton(4).Value
    15.             MsgBoxStyle = vbRetryCancel
    16.             txtResult.Text = "Retry and cancel"
    17.         Case optButton(5).Value
    18.             MsgBoxStyle = vbAbortRetryIgnore
    19.             txtResult.Text = "Abort, Retry and Ignore"
    20.     End Select
    21.  
    22.     Select Case True
    23.         Case optIcon(0).Value
    24.             MsgBoxStyle = MsgBoxStyle
    25.         Case optIcon(1).Value
    26.             MsgBoxStyle = MsgBoxStyle + vbCritical
    27.             txtResult.Text = txtResult.Text & "+ Critical"
    28.         Case optIcon(2).Value
    29.             MsgBoxStyle = MsgBoxStyle + vbExclamation
    30.             txtResult.Text = txtResult.Text & "+ Exclamation"
    31.         Case optIcon(3).Value
    32.             MsgBoxStyle = MsgBoxStyle + vbCritical
    33.             txtResult.Text = txtResult.Text & "+ Critical"
    34.         Case optIcon(4).Value
    35.             MsgBoxStyle = MsgBoxStyle + vbInformation
    36.             txtResult.Text = txtResult.Text & "+ Information"
    37.         Case optIcon(5).Value
    38.             MsgBoxStyle = MsgBoxStyle + vbInformation
    39.             txtResult.Text = txtResult.Text & "+ Information"
    40.         Case optIcon(6).Value
    41.             MsgBoxStyle = MsgBoxStyle + vbQuestion
    42.             txtResult.Text = txtResult.Text & "+ Question"
    43.         Case optIcon(7).Value
    44.             MsgBoxStyle = MsgBoxStyle + vbCritical
    45.             txtResult.Text = txtResult.Text & "+ Critical"
    46.         Case optIcon(8).Value
    47.             MsgBoxStyle = MsgBoxStyle + vbExclamation
    48.             txtResult.Text = txtResult.Text & "+ Exclamation"
    49.     End If
    50.  
    51.                
    52.     MsgBox txtMessage.Text, MsgBoxStyle, txtTitle.Text
    BTW, are you sure you want the optIcon values set up the way you have them? For example both 4 and 5 are vbInformation.

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