miércoles, 18 de marzo de 2015

Funciones con parámetro de una variable como para metro una variable

Crear una aplicación de VB.NET, para el promedio de tres números ingresados manualmente. crear una función para hacer el promedio y que retorne el resultado.


 Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TpUNTO.Text = (Val(TextBox1.Text) + Val(TextBox2.Text) + Val(TextBox3.Text) & "  Puntos")
        Dim funReci As Decimal
        promedio.Text = calcular(funReci)
    End Sub
    Function calcular(ByVal recivir As Decimal)
        recivir = FormatNumber(((Val(TextBox1.Text) + Val(TextBox2.Text) + Val(TextBox3.Text)) / 3), 1)
        Return recivir
    End Function
End Class

lunes, 2 de marzo de 2015

Generar una matriz de números y mostrar la diagonal proncipal

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim areglo(4, 4) As Integer
        Dim i, j As Integer
        Dim contador As Integer
        Dim promp As String = "Arreglo bidimencional"
        Dim mensaje As String = "Leer los elementos del arreglo"
        Dim text As TextBox() = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10, TextBox11, TextBox12, TextBox13, TextBox14, TextBox15, TextBox16}
        Dim suma As Integer = 0
        Dim prom As Decimal = 0
        Dim mayor As Integer = 0
        contador = 0
        For i = 0 To 3
            For j = 0 To 3
                areglo(j, i) = InputBox(mensaje, promp)
                text(contador).Text = areglo(j, i)
                contador = contador + 1
                If mayor < areglo(j, i) Then
                    mayor = areglo(j, i)

                End If

            Next j

        Next i
        suma = areglo(0, 0) + areglo(1, 1) + areglo(2, 2) + areglo(3, 3)
        prom = suma / 4
        MsgBox("Los numeros capturados de la diagonal son:  " & areglo(0, 0) & " - " & areglo(1, 1) & " - " & areglo(2, 2) & " - " & areglo(3, 3))

    End Sub
End Class

APLICACION PARA SUMAR DOS AREGLOS EN VB.NET

Public Class Form1
    Dim r As New Random
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim a(9) As Integer
        Dim b(9) As Integer
        ListBox1.DataSource = llenarArreglo(a)
        ListBox2.DataSource = llenarArreglo(b)
        ListBox3.DataSource = sumarArreglos(a, b)
    End Sub
    Function llenarArreglo(ByRef c() As Integer) As Array
        For i = 0 To c.GetUpperBound(0)
            c(i) = r.Next(10, 99)
        Next
        Return c
    End Function
    Function sumarArreglos(x() As Integer, y() As Integer) As Array
        Dim c(0) As Integer
        If x.Length = y.Length Then
            ReDim c(x.GetUpperBound(0))
            For i = 0 To x.GetUpperBound(0)
                c(i) = x(i) + y(i)
            Next
        End If
        Return c
    End Function
End Class

Generar claves de forma automática en windows form

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Label1.Text = generarClave(4, 65, 90) & generarClave(4, 48, 57)
    End Sub
    Function generarClave(n As Integer, li As Integer, ls As Integer) As String
        Dim cadena As String = ""
        Dim car As String = ""
        Dim r As New Random
        For i = 1 To n
            car = Chr(r.Next(li, ls))
            While InStr(cadena, car)
                car = Chr(r.Next(li, ls))
            End While
            cadena = cadena & car
        Next
        Return cadena
    End Function
End Class

domingo, 1 de marzo de 2015

Generar numeros aleatorios en windows form

Public Class Form2
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim r As New Random
        For i = 1 To 15
            Dim num As Integer = r.Next(10, 99)
            ListBox1.Items.Add(num)
        Next
        Label1.Text = "Suma: " & contar(ListBox1)
    End Sub
    Function contar(lista As ListBox) As Integer
        Dim s As Integer = 0
        For Each num In lista.Items
            s = s + num
        Next
        Return s
    End Function
End Class

Aplicacion de windows form simular un cajero automatico


Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Label1.Text = Label1.Text & numeroCuenta
        Label2.Text = Label2.Text & saldoActual
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If RadioButton1.Checked Then
            If IsNumeric(TextBox1.Text) And Val(TextBox1.Text) > 0 Then
                deposito(TextBox1.Text)
                Label2.Text = "Saldo actual: " & saldoActual
            Else
                MsgBox("Escriba una cantidad positiva")
                TextBox1.Clear()
                TextBox1.Focus()
            End If
        Else
            If IsNumeric(TextBox1.Text) And Val(TextBox1.Text) > 0 Then
                Label2.Text = "Saldo actual: " & retiro(TextBox1.Text)
            Else
                MsgBox("Escriba una cantidad positiva")
                TextBox1.Clear()
                TextBox1.Focus()
            End If
        End If
    End Sub
End Class

PONER EN UN MODULO EL SIGUIENTE  CÓDIGO
Module Module1
    Public numeroCuenta As String = "763846238"
    Public saldoActual As Decimal = 1200

    Sub deposito(cantidad As Decimal)
        If cantidad > 0 Then
            saldoActual = saldoActual + cantidad
        End If
    End Sub

    Function retiro(cantidad As Decimal) As Decimal
        If cantidad > 0 And cantidad <= saldoActual Then
            saldoActual = saldoActual - cantidad
        End If
        Return saldoActual
    End Function

End Module

Ejemplo de data DataGridView


Ejemplo: Desarrollar una plicacion de windows con un data  DataGridView. 
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Not IsNumeric(txtCantidad.Text) Or cmbTipo.Text = String.Empty Then
            MsgBox("Debe introducir una cantidad y seleccionar el tipo")
            txtCantidad.Focus()
        Else
            Dim cant As Integer = txtCantidad.Text
            Dim tipo As String = cmbTipo.Text
            Dim prec As Decimal
            Dim subt As Decimal
            If tipo = "Negro" Then
                prec = 0.05
            Else
                prec = 0.15
            End If
            subt = cant * prec
            DataGridView1.Rows.Add(cant, tipo, prec, subt)

            Dim total As Decimal = 0
            For f = 0 To DataGridView1.Rows.Count - 1
                total = total + DataGridView1.Rows(f).Cells(3).Value
            Next
            txtTotal.Text = FormatCurrency(total, 2)
        End If
    End Sub
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        DataGridView1.Rows.Clear()
        txtCantidad.Clear()
        cmbTipo.SelectedIndex = -1
        txtTotal.Clear()
        txtCantidad.Focus()
    End Sub
    Private Sub txtCantidad_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtCantidad.KeyPress
        If InStr("0123456789", e.KeyChar) = False Then
            If Asc(e.KeyChar) <> 8 Then
                e.Handled = True
            End If
        End If
    End Sub
    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        End
    End Sub
End Class