Search code examples
vb.netradio-buttonmsgbox

VB.NET How to display a msgbox if no radiobuttons were checked?


Im creating a Payment Method, but im unsure how to display a msgbox when none of the Radiobuttons are checked.

This is my current code.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If RadioButton1.Checked Then Paypal_Pay.Show()
    Me.Hide()

    If RadioButton2.Checked Then PayWave_Pay.Show()
    Me.Hide()

    If RadioButton3.Checked Then CreditCard_Pay.Show()
    Me.Hide()
End Sub

But im unsure how to implement the msgbox in when the user doesn't check any of the radiobuttons. Sidenote- If the User clicks the Button and doesn't check any of the button, it just crashes.. so im unsure how to fix that.


Solution

  • You can use If..ElseIf to test each RadioButton, and then have a final Else to display the MsgBox, eg:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      If RadioButton1.Checked Then
        Paypal_Pay.Show()
        Me.Hide()
      ElseIf RadioButton2.Checked Then
        PayWave_Pay.Show()
        Me.Hide()
      ElseIf RadioButton3.Checked Then
        CreditCard_Pay.Show()
        Me.Hide()
      Else
        MsgBox(...)
      End If
    End Sub