jueves, 13 de junio de 2013

PROGRAMACION EN VISUAL BASIC.NET



DISEÑAR LOS SIGUIENTES FORMULARIOS

1.      En el siguiente formulario se realiza un pequeño ejemplo de un mantenimiento con procedimientos almacenados realizados en sql server 2008.

 

Luego se realiza el siguiente código:

OBJETO: GENERAL                  EVENTO: DECLARACIONES  

Imports System.Data
Imports System.Data.SqlClient
OBJETO: Form1                 EVENTO: DECLARACIONES  

    Dim da As New SqlDataAdapter("select * from categoria", cn)

CREAR LA FUNCION LIMPIAR:

    Sub limpiar()
        Dim con As Control
        For Each con In Me.Panel1.Controls
            If TypeOf con Is TextBox Then con.Text = ""
        Next
    End Sub

CREAR LA FUNCION HABILITAR:

    Sub habilitar(ByVal dito As Boolean)
        Me.BTNNUEVO.Enabled = dito
        Me.BTNEDITAR.Enabled = dito
        Me.BTNGUARDAR.Enabled = Not dito
        Me.BTNBUSCAR.Enabled = dito
        Me.BTNCANCELAR.Enabled = Not dito
        Me.BTNSALIR.Enabled = dito
        Me.Panel1.Enabled = Not dito
    End Sub

CREAR LA FUNCION LIMPIAR:

    Sub cargardatos()
        Dim cmd As New SqlCommand("UspListarcat", cn)
        cmd.CommandType = CommandType.StoredProcedure
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
    End Sub
   
OBJETO: Form1                 EVENTO: LOAD

        habilitar(True)
        cargardatos()
        Me.Width = 382
        Me.TXTCOD.Enabled = False
        DataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically


OBJETO: BTNNUEVO                EVENTO: CLICK

        TXTCOD.Text = generar("CATEGORIA", "COD_CAT")
        habilitar(False)
        limpiar()
        Me.TXTNOM.Focus()

OBJETO: BTNEDITAR               EVENTO: CLICK

        BTNGUARDAR.Text = "ACTUALIZAR"
        If Me.TXTCOD.Text.Trim = "" Then
            MessageBox.Show("seleccione la categoria a modificar", "SELECCIONE", MessageBoxButtons.OK, _
            MessageBoxIcon.Exclamation)
        Else
            habilitar(False)
        End If

OBJETO: BTNGUARDAR                EVENTO: CLICK

        If Me.BTNGUARDAR.Text.Length < 10 Then
            Try
                Dim cmd As New SqlCommand("uspGuardarcat", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add(New SqlParameter("@cod_CAT", SqlDbType.Char, 10, ParameterDirection.Input)).Value = (TXTCOD.Text).ToUpper
                cmd.Parameters.Add(New SqlParameter("@NOM_CAT", SqlDbType.NVarChar, 50, ParameterDirection.Input)).Value = (TXTNOM.Text).ToUpper
                BTNGUARDAR.Text = "GUARDAR"
                cn.Open()
                cmd.ExecuteNonQuery()
                MessageBox.Show("La Categoria se Guardó con Éxito", "GUARDAR", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("Error al Guardar la Categoria", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                cn.Close()
            End Try
        Else
            Try
                Dim cmd As New SqlCommand("USPMODIFICARCAT", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@cod_cat", SqlDbType.Char, 10).Value = Me.TXTCOD.Text.ToUpper
                cmd.Parameters.Add("@Nom_cat", SqlDbType.NVarChar, 50).Value = Me.TXTNOM.Text.ToUpper
                If cn.State = ConnectionState.Open Then cn.Close()
                cn.Open()
                cmd.ExecuteNonQuery()
                MessageBox.Show("La Categoria se Modificó con Éxito", "MODIFICAR", MessageBoxButtons.OK, _
                MessageBoxIcon.Information)
                Me.BTNGUARDAR.Text = "GUARDAR"
            Catch ex As Exception
                MessageBox.Show("Error al Modificar la categoria", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Finally
                If cn.State = ConnectionState.Open Then cn.Close()
            End Try
        End If
        limpiar()
        habilitar(True)

OBJETO: BTNBUSCAR                EVENTO: CLICK

        Me.TXTBUSCAR_TextChanged(sender, e)
        Me.Width = 755
        Me.TXTBUSCAR.Focus()

OBJETO: BTNCANCELAR                EVENTO: CLICK

        habilitar(True)
        limpiar()
        TXTCOD.Text = ""

OBJETO: TXTBUSCAR               EVENTO: TEXTCHANGE

        Dim cmd As New SqlCommand("UspFiltrarprod", cn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@nom_cat", SqlDbType.VarChar, 50).Value = Me.TXTBUSCAR.Text
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
        Me.DataGridView1.DataSource = dt
   
OBJETO: DataGridView1                EVENTO: CLICK

        Dim Pos As Integer
        Pos = DataGridView1.CurrentRow.Index
        DataGridView1.Rows(Pos).Selected = True
        Me.TXTCOD.Text = DataGridView1.Rows(Pos).Cells(0).Value
        Me.TXTNOM.Text = DataGridView1.Rows(Pos).Cells(1).Value
        Me.Width = 382
   
OBJETO: BTNSALIR                EVENTO: CLICK

            Close()

CREAR UN MODULO EN EL CUAL IRA LA CONEXION A LA BASE DE DATOS

Imports System.Data.SqlClient
Imports System.Data
Module Conexion
    Public daproductos As New SqlDataAdapter("select * from CLIENTE", cn)
    Public ds As New DataSet
    Public cn As New SqlConnection("data source=(local);integrated security=true;initial catalog=lavanderia")
Public Function generar(ByVal nombretabla As String, ByVal campogenera As String)
        Dim dagenerar As New SqlDataAdapter
        Dim dsge As New DataSet
        Dim codigogenerado As String = ""
        dagenerar.SelectCommand = New SqlCommand
        With dagenerar.SelectCommand
            .Connection = cn
            .CommandType = CommandType.Text
            .CommandText = "select max(" & campogenera & ") from " & nombretabla.Trim
        End With
        Try
            cn.Open()
            dagenerar.Fill(dsge, "codigo")
            cn.Close()
            codigogenerado = (dsge.Tables("codigo").Rows(0)(0)).ToString
            If codigogenerado <> "" Then
                codigogenerado = dsge.Tables("codigo").Rows(0)(0)
                codigogenerado = Left(codigogenerado, 7 - CStr(Val(Right(codigogenerado, 7) + 1)).Length) & CStr(Val(Right(codigogenerado, 7)) + 1)
            Else
                codigogenerado = CStr(Left(nombretabla, 3).ToUpper) & "0001"
            End If
        Catch EX As Exception
            MsgBox(EX.Message)
        End Try
        Return codigogenerado
    End Function
End Module

DISEÑAR EL SIGUIENTE FORMULARIO
  
Este codigo sirve para programar la hora en la que deseas que se apague tu computadora.


OBJETO: BTNSALIR                EVENTO: CLICK

Imports System.Data.SqlClient
Public Class apagarcom
    Inherits System.Windows.Forms.Form
   
OBJETO: BTNSTART               EVENTO: CLICK

        Me.CmdStart.Enabled = False
        Me.CmdParar.Enabled = True
        Me.txtHora.Enabled = False
        Me.txtMinutos.Enabled = False
        Timer1.Enabled = True
        Timer1.Start()
   
OBJETO: BTNSTOP             EVENTO: CLICK

        If CType(Me.txtHora.Text, Int32) = CType(Date.Now.Hour, Int32) Then
            If CType(Me.txtMinutos.Text, Int32) = CType(Date.Now.Minute, Int32) Then
                Shutdown()
            End If
        End If
        
OBJETO: TIMER1                        EVENTO: TICK

        If CType(Me.txtHora.Text, Int32) = CType(Date.Now.Hour, Int32) Then
            If CType(Me.txtMinutos.Text, Int32) = CType(Date.Now.Minute, Int32) Then
                Shutdown()
            End If
        End If
        
OBJETO: FORM 2             EVENTO: LOAD

        Me.txtHora.Text = CStr(Date.Now.Hour.ToString)
        Me.txtMinutos.Text = CStr(Date.Now.Minute.ToString)







Espero que les haya servido en algo estos pequeños codigos 

0 comentarios:

Publicar un comentario