locked
Visual Basic language syntax RRS feed

  • Question

  • I want to create a MsgBox function using the buttons Ok and Cancel.  I know how to do it but what syntax do I use when the input is Cancel to go back to the program?

    Friday, March 12, 2010 3:20 AM

Answers

  • Hi,

    When the code reaches END SUB it returns back to the program anyway unless it was called from within another SUB or FUNCTION which would then return program execution to the line of code immediately following where the SUB to show the MessageBox was called.  :-)   :-D

    So you can use.>>

    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim result As DialogResult
            'result = MsgBox("Your prompt here.", MsgBoxStyle.OkCancel, "MessageBox title here.")
    
            'Use MessageBox.Show as it has a lot more options than the old MsgBox .>>
            result = MessageBox.Show("MessageBox text here.", "Messagebox title here.", MessageBoxButtons.OKCancel)
    
            If result = Windows.Forms.DialogResult.OK Then
                'Your code here.>>
    
            ElseIf result = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If
    
        End Sub
    End Class


    or simply use .>>

    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim result As DialogResult
            'result = MsgBox("Your prompt here.", MsgBoxStyle.OkCancel, "MessageBox title here.")
    
            'Use MessageBox.Show as it has a lot more options than the old MsgBox .>>
            result = MessageBox.Show("MessageBox text here.", "Messagebox title here.", MessageBoxButtons.OKCancel)
    
            If result = Windows.Forms.DialogResult.OK Then
                'Your code here.>>
    
            End If
    
        End Sub
    End Class


    Regards,

    John
    • Marked as answer by dheardsr Friday, March 12, 2010 1:06 PM
    Friday, March 12, 2010 4:10 AM

All replies

  • Hi,

    When the code reaches END SUB it returns back to the program anyway unless it was called from within another SUB or FUNCTION which would then return program execution to the line of code immediately following where the SUB to show the MessageBox was called.  :-)   :-D

    So you can use.>>

    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim result As DialogResult
            'result = MsgBox("Your prompt here.", MsgBoxStyle.OkCancel, "MessageBox title here.")
    
            'Use MessageBox.Show as it has a lot more options than the old MsgBox .>>
            result = MessageBox.Show("MessageBox text here.", "Messagebox title here.", MessageBoxButtons.OKCancel)
    
            If result = Windows.Forms.DialogResult.OK Then
                'Your code here.>>
    
            ElseIf result = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If
    
        End Sub
    End Class


    or simply use .>>

    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim result As DialogResult
            'result = MsgBox("Your prompt here.", MsgBoxStyle.OkCancel, "MessageBox title here.")
    
            'Use MessageBox.Show as it has a lot more options than the old MsgBox .>>
            result = MessageBox.Show("MessageBox text here.", "Messagebox title here.", MessageBoxButtons.OKCancel)
    
            If result = Windows.Forms.DialogResult.OK Then
                'Your code here.>>
    
            End If
    
        End Sub
    End Class


    Regards,

    John
    • Marked as answer by dheardsr Friday, March 12, 2010 1:06 PM
    Friday, March 12, 2010 4:10 AM
  • Thanks John.  I'll give this a try.  I may have to come back.
    Friday, March 12, 2010 1:05 PM
  • Yes John it worked, thanks.  Exit Sub is what I was looking for.
    Friday, March 12, 2010 6:55 PM