Prevent an MsgBox from repeating twice in VBA

0

I do not know why my MsgBox is duplicated twice, it is supposed that when the spaces are blank send a message that they are blank, all this when pressing a radio button, the first time it works, but in the Second, the third one already shows the message 2 times. I leave the code:

 Function validarBlancos(ByVal textoUno As String, ByVal textoDos As String) As Boolean

    Return String.IsNullOrEmpty(textoUno) Or String.IsNullOrEmpty(textoDos)

End Function

and in the following code I call the function:

Private Sub rbtnSumar_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnSumar.CheckedChanged

    MsgBox("bandera suma")
    If Not validarBlancos(txtNumeroUno.Text.Trim, txtNumeroDos.Text.Trim) Then
        numeroUno = Integer.Parse(txtNumeroUno.Text)
        numeroDos = Integer.Parse(txtNumeroDos.Text)
        If rbtnSumar.Checked = True Then
            txtResultado.Text = numeroUno + numeroDos
        End If
    Else
        MsgBox("Espacio en blanco... Reintente", vbExclamation, "Espacio en blanco,suma")
    End If

End Sub

Private Sub rbtnRestar_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnRestar.CheckedChanged

    MsgBox("bandera resta")
    If Not validarBlancos(txtNumeroUno.Text.Trim, txtNumeroDos.Text.Trim) Then
        numeroUno = Integer.Parse(txtNumeroUno.Text)
        numeroDos = Integer.Parse(txtNumeroDos.Text)
        If rbtnRestar.Checked = True Then
            txtResultado.Text = numeroUno - numeroDos
        End If
    Else
        MsgBox("Espacio en blanco... Reintente",, "Espacio en blanco,resta")
    End If

End Sub

Private Sub rbtnMultiplicar_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnMultiplicar.CheckedChanged

    MsgBox("bandera multi")
    If Not validarBlancos(txtNumeroUno.Text.Trim, txtNumeroDos.Text.Trim) Then
        numeroUno = Integer.Parse(txtNumeroUno.Text)
        numeroDos = Integer.Parse(txtNumeroDos.Text)
        If rbtnMultiplicar.Checked = True Then
            txtResultado.Text = numeroUno * numeroDos
        End If
    Else
        MsgBox("Espacio en blanco... Reintente",, "Espacio en blanco,multi")
    End If

End Sub

Private Sub rbtnDividir_CheckedChanged(sender As Object, e As EventArgs) Handles rbtnDividir.CheckedChanged

    MsgBox("bandera dividir")
    If Not validarBlancos(txtNumeroUno.Text.Trim, txtNumeroDos.Text.Trim) Then
        numeroUno = Integer.Parse(txtNumeroUno.Text)
        numeroDos = Integer.Parse(txtNumeroDos.Text)
        If rbtnDividir.Checked = True Then
            If numeroDos = 0 Then
                MsgBox("No puede dividir por 0, Reintente")
                txtNumeroDos.Clear()
            Else
                txtResultado.Text = numeroUno / numeroDos
            End If
        End If
    Else
        MsgBox("Espacio en blanco... Reintente",, "Espacio en blanco,dividir")
    End If

End Sub
    
asked by GinoGiovanni 11.10.2016 в 04:00
source

1 answer

2

Some considerations in your code to take into account to improve and avoid these inconveniences

  • It would be better if the verification of two variables is done using the logical operator OR
  • The most optimal and appropriate way to verify empty variables and also null is with the method IsNullOrEmpty
  • Consider using the Trim to remove blank spaces from the ends of the string
  • When you insert a method that returns a value booleano in a if , you do not need validation =True without that you already understand that it will verify that it is True if you could not use the operator denial Not (Optional)
  • The Message you put in your method should go in the else of your check in the CheckedChanged method
  • Your method returns no value booleano (as Boolean) as specified by the documentation link
  • Your code would be like this.

    /* Funcion con retorno Booleano (as Boolean)-> Tipo retorno */
    Function validarBlancos(ByVal textoUno As String, ByVal textoDos As String) As Boolean
        /* Un solo return , evitamos el if else */
        Return String.IsNullOrEmpty(textoUno) Or String.IsNullOrEmpty(textoDos)
    End Function
    
    
    
    
    Private Sub rbtnSumar_Click(sender As Object, e As EventArgs) Handles rbtnSumar.CheckedChanged
        /* Esto sería igual a decir , si no hay(Not) espacios en blanco */
        If Not validarBlancos(txtNumeroUno.Text.Trim, txtNumeroDos.Text.Trim) Then
            numeroUno = Integer.Parse(txtNumeroUno.Text)
            numeroDos = Integer.Parse(txtNumeroDos.Text)
            If rbtnSumar.Checked = True Then
                txtResultado.Text = numeroUno + numeroDos
            End If
        Else
            MsgBox("Espacio en blanco... Reintente")
        End If
    End Sub
    

    Update

    The problem with the two messages is that when a RadioButton1.Checked = True and you change to RadioButton2.Checked = True %% of% the event RadioButton1.Checked = False of the two is launched. that's why it launches the two CheckedChanged (Two messages)

    A quick solution would be the code to add it to the event else of the Click Another would be to verify if this Checkbox the Checked=True

     Private Sub rbtnSumar_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        If rbtnSumar.Checked = True Then
            If Not validarBlancos(txtNumeroUno.Text.Trim, txtNumeroDos.Text.Trim) Then
                 numeroUno = Integer.Parse(txtNumeroUno.Text)
                  numeroDos = Integer.Parse(txtNumeroDos.Text)
                 If rbtnMultiplicar.Checked = True Then
                  txtResultado.Text = numeroUno * numeroDos
                 End If
            Else
                MsgBox("Espacio en blanco... Reintente", vbExclamation, "Espacio en blanco,suma")
            End If
        End If
    End Sub
    
        
    answered by 11.10.2016 / 08:03
    source