You are on page 1of 2

CODIGO EJERCICIO OPERACIONES ARITMETICAS, SELECCIN CON COMBO BOX

Public Class principal Dim num1, num2, resul As Single 'Variables definidas como publicas o globales Private Sub btn_sumar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sumar.Click Dim a As Control For Each a In Me.Controls If TypeOf a Is TextBox Then a.Text = Nothing End If Next End Sub Private Sub txtnumero1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnumero1.KeyPress 'si el dato ingresado no es numerico, es distinto de los valores ascii 13 y 8(enter y backspace) If Not IsNumeric(e.KeyChar) And Asc(e.KeyChar) <> 13 And Asc(e.KeyChar) <> 8 Then MessageBox.Show("Porfavor ingrese solo numeros") 'la propiedad de Handled se establece en true, que indica que el evento se controla e.Handled = True Else ' Si se pulsa la tecla Intro, pasar al siguiente TextBox If e.KeyChar = Convert.ToChar(Keys.Return) Then 'e.KeyChar obtiene o establece el carcter correspondiente a la tecla presionada e.Handled = True txtnumero2.Focus() End If End If End Sub Private Sub cb_tipo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb_tipo.SelectedIndexChanged Dim op As Single Dim operacion As String 'Dim mt, desc, tot As Single num1 = CSng(txtnumero1.Text) num2 = CSng(txtnumero2.Text) 'Aqui la seleccion del ComboBox se muestra en un MessageBox operacion = CStr(cb_tipo.SelectedItem) MessageBox.Show("Selecciono " & operacion) 'Aqui en la variable op se guarda el numero de la posicion seleccionada 'en el ComboBox op = Me.cb_tipo.SelectedIndex

'Ahora se valida la seleccion del ComboBox, recuerde que los items estan 'ordenados por la propiedad Sort Select op 'si selecciono el primer elemento de la lista Case 0 resul = num1 + num2 Case 1 resul = num1 - num2 Case 2 resul = num1 * num2 Case 3 resul = num1 / num2 End Select txtresultado.Text = CStr(resul) End Sub End Class

You might also like