Free Web Site - Free Web Space and Site Hosting - Web Hosting - Internet Store and Ecommerce Solution Provider - High Speed Internet
Search the Web

Guia 4 de ejercicios

Profesor : Manuel Diaz
Ayudante : Pedro Silva



Bajar Codigo Fuente de la guia

1) Implementar un programa que dibuje un circulo, cuadrado o rectangulo.
La forma se deberá escojer de un ListBox y las opciones serán determinadas en tiempo de diseño
El color (verde, amarillo, rojo ) se debera seleccionar desde tres OptionButton






SOLUCION
Begin VB.Form Form1 
   Caption         =   "Formulario de Dibujo"
   ClientHeight    =   2940
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   7740
   LinkTopic       =   "Form1"
   ScaleHeight     =   2940
   ScaleWidth      =   7740
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdDibujar 
      Caption         =   "Dibujar"
      Height          =   495
      Left            =   1800
      TabIndex        =   5
      Top             =   1920
      Width           =   1575
   End
   Begin VB.OptionButton optRojo 
      Caption         =   "Rojo"
      Height          =   255
      Left            =   360
      TabIndex        =   4
      Top             =   2400
      Width           =   975
   End
   Begin VB.OptionButton optAmarillo 
      Caption         =   "Amarillo"
      Height          =   255
      Left            =   360
      TabIndex        =   3
      Top             =   2040
      Width           =   975
   End
   Begin VB.OptionButton optVerde 
      Caption         =   "Verde"
      Height          =   255
      Left            =   360
      TabIndex        =   2
      Top             =   1680
      Value           =   -1  'True
      Width           =   975
   End
   Begin VB.ListBox ListForma 
      Height          =   645
      ItemData        =   "frmDibujo.frx":0000
      Left            =   360
      List            =   "Cuadrado","Rectangulo","Circulo"
      TabIndex        =   1
      Top             =   720
      Width           =   1815
   End
   Begin VB.Label lblDibujo 
      Caption         =   "DIBUJO"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   13.5
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   5160
      TabIndex        =   0
      Top             =   240
      Width           =   1215
   End
   Begin VB.Shape ShapeDibujo 
      FillStyle       =   0  'Solid
      Height          =   1215
      Left            =   4680
      Top             =   1080
      Width           =   1935
   End
End
Private Sub cmdDibujar_Click()

    If Me.ListForma.ListIndex = -1 Then
        MsgBox "No hay forma seleccionada"
    Else
        Select Case Me.ListForma.ListIndex
    
            Case 0
                Me.ShapeDibujo.Shape = vbShapeSquare
            Case 1
                Me.ShapeDibujo.Shape = vbShapeRectangle
            Case 2
                Me.ShapeDibujo.Shape = vbShapeCircle
        End Select
    
    
        If Me.optVerde.Value = True Then
            Me.ShapeDibujo.FillColor = vbGreen
        End If
    
        If Me.optAmarillo.Value = True Then
            Me.ShapeDibujo.FillColor = vbYellow
        End If
    
        If Me.optRojo.Value = True Then
            Me.ShapeDibujo.FillColor = vbRed
        End If
    
    End If

End Sub



2) Implementar un formulario, que asigne aleatoriamente números aleatorios [ 0..1] cada cierto intervalo de tiempo (modificable), a dos TextBox.
También, se tiene que incluir un textbox que muestre la multiplicación de los dos factores.
Si cualquiera de los factores, es mayor o igual a 0.5, debe ser mostrado con color verde. En caso contrario, con color Rojo.
Finalmente, debe incluir un ScrollBar y un Timer para sincronizar, el intervalo de tiempo requerido para generar una nueva combinación de números aleatorios.





Begin VB.Form Form1 
   Caption         =   "Formulario de activacion aleatoria"
   ClientHeight    =   2865
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   10260
   LinkTopic       =   "Form1"
   ScaleHeight     =   2865
   ScaleWidth      =   10260
   StartUpPosition =   3  'Windows Default
   Begin VB.HScrollBar HScroll1 
      Height          =   255
      LargeChange     =   10
      Left            =   960
      Max             =   1000
      Min             =   50
      SmallChange     =   10
      TabIndex        =   8
      Top             =   2040
      Value           =   500
      Width           =   2295
   End
   Begin VB.CommandButton cmdContinuar 
      Caption         =   "Continuar"
      Enabled         =   0   'False
      Height          =   495
      Left            =   7800
      TabIndex        =   4
      Top             =   1920
      Width           =   1575
   End
   Begin VB.CommandButton cmdDetener 
      Caption         =   "Detener"
      Height          =   495
      Left            =   5760
      TabIndex        =   3
      Top             =   1920
      Width           =   1695
   End
   Begin VB.Timer Timer1 
      Interval        =   450
      Left            =   4200
      Top             =   1440
   End
   Begin VB.Label Label4 
      Caption         =   "Intervalo de tiempo en ms"
      Height          =   255
      Left            =   1080
      TabIndex        =   10
      Top             =   1800
      Width           =   2295
   End
   Begin VB.Label lblintervalo 
      Caption         =   "Intervalo de tiempo"
      Height          =   255
      Left            =   3480
      TabIndex        =   9
      Top             =   2040
      Width           =   1215
   End
   Begin VB.Label Label3 
      Caption         =   "X * Y"
      Height          =   255
      Left            =   5880
      TabIndex        =   7
      Top             =   240
      Width           =   1935
   End
   Begin VB.Label Label2 
      Caption         =   "Y"
      Height          =   255
      Left            =   360
      TabIndex        =   6
      Top             =   1080
      Width           =   255
   End
   Begin VB.Label Label1 
      Caption         =   "X"
      Height          =   255
      Left            =   360
      TabIndex        =   5
      Top             =   480
      Width           =   255
   End
   Begin VB.Label LabelZ 
      BackColor       =   &H00000000&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Z"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   615
      Left            =   5760
      TabIndex        =   2
      Top             =   600
      Width           =   4095
   End
   Begin VB.Line Line2 
      X1              =   3240
      X2              =   5640
      Y1              =   600
      Y2              =   720
   End
   Begin VB.Line Line1 
      X1              =   3240
      X2              =   5640
      Y1              =   1200
      Y2              =   960
   End
   Begin VB.Label LabelY 
      BackColor       =   &H00404040&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Y"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   840
      TabIndex        =   1
      Top             =   960
      Width           =   2295
   End
   Begin VB.Label LabelX 
      BackColor       =   &H00404040&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "X"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   840
      TabIndex        =   0
      Top             =   360
      Width           =   2295
   End
End
Dim x As Double, y As Double, z As Double

Private Sub cmdContinuar_Click()

    Timer1.Enabled = True
    Me.cmdDetener.Enabled = True
    Me.cmdContinuar.Enabled = False
End Sub

Private Sub cmdDetener_Click()

    Timer1.Enabled = False
    Me.cmdContinuar.Enabled = True
    Me.cmdDetener.Enabled = False
End Sub

Private Sub Form_Load()
    Randomize
    Me.lblintervalo.Caption = Me.HScroll1.Value
    Me.Timer1.Interval = Me.HScroll1.Value
End Sub


Private Sub HScroll1_Change()
    
    Me.Timer1.Interval = Me.HScroll1.Value
    Me.lblintervalo = Me.HScroll1.Value
   
End Sub

Private Sub Timer1_Timer()

    x = Round(Rnd, 5)
    y = Round(Rnd, 5)
    z = Round(x * y, 5)

    Me.LabelX = Format(x, "#0.#####")
    Me.LabelY = Format(y, "#0.#####")
    
    If CDbl(LabelX.Caption) < 0.5 Then
        Me.LabelX.ForeColor = vbRed
    Else
        Me.LabelX.ForeColor = vbGreen
    End If
    
    
    If CDbl(LabelY.Caption) < 0.5 Then
        Me.LabelY.ForeColor = vbRed
    Else
        Me.LabelY.ForeColor = vbGreen
    End If
    
    If z < 0.5 Then
        Me.LabelZ.ForeColor = vbRed
    Else
        Me.LabelZ.ForeColor = vbGreen
    End If
    
    
    
    If z < 0.5 Then
        Me.LabelZ.Caption = "DESACTIVADO : "
    Else
        Me.LabelZ.Caption = "ACTIVADO : "
    End If

    Me.LabelZ.Caption = Me.LabelZ.Caption & Format(z, "#0.#####")
    


End Sub



3) Implementar un tabla de 3 x 3 entradas numericas con el control MSFlexgrid, en donde se asigne aleatoriamente números enteros en un intervalo de [-10,10], para calcular las sumas por fila y por columna.
Cada asignación aleatoria por fila, será activada por un Reloj, usando el control Timer.
Si la suma por fila y columna es igual, se activará un circulo de color verde
Si la suma es diferente, se activará un circulo de color rojo, y además se desactivarán los relojes
También se debe implementar un botón de comando, para hacer pausa en los relojes





Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3285
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7905
   LinkTopic       =   "Form1"
   ScaleHeight     =   3285
   ScaleWidth      =   7905
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdDetener 
      Caption         =   "Pausa"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   615
      Left            =   2520
      TabIndex        =   1
      Top             =   2040
      Width           =   2175
   End
   Begin VB.Timer Reloj 
      Index           =   2
      Left            =   360
      Top             =   1440
   End
   Begin VB.Timer Reloj 
      Index           =   1
      Left            =   360
      Top             =   960
   End
   Begin VB.Timer Reloj 
      Index           =   0
      Left            =   360
      Top             =   480
   End
   Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1 
      Height          =   1455
      Left            =   1080
      TabIndex        =   0
      Top             =   360
      Width           =   5055
      _ExtentX        =   8916
      _ExtentY        =   2566
      _Version        =   393216
      Rows            =   5
      Cols            =   5
      AllowUserResizing =   1
      FormatString    =   ""
   End
   Begin VB.Shape Figura 
      FillColor       =   &H000000FF&
      FillStyle       =   0  'Solid
      Height          =   975
      Left            =   6480
      Shape           =   3  'Circle
      Top             =   480
      Width           =   1095
   End
End
Private Sub Form_Load()

    Dim i As Integer, j As Integer

    With Me.MSFlexGrid1
        .TextMatrix(0, 1) = "col 1"
        .TextMatrix(0, 2) = "col 2"
        .TextMatrix(0, 3) = "col 3"
        .TextMatrix(0, 4) = "TOTAL"

        .TextMatrix(1, 0) = "fil 1"
        .TextMatrix(2, 0) = "fil 2"
        .TextMatrix(3, 0) = "fil 3"
        .TextMatrix(4, 0) = "TOTAL"
    
        For i = 1 To 3
            For j = 1 To 3
                .TextMatrix(i, j) = "0"
            Next j
        Next i
    End With

    ' un reloj por cada fila

    Reloj(0).Interval = 50
    Reloj(1).Interval = 150
    Reloj(2).Interval = 500


End Sub

Private Function generarNumAleatorio(ByVal limInf As Integer, ByVal limSup As Integer) As Integer
    
    Dim generado  As Boolean, num As Integer
    Dim cont As Integer
    
    cont = 0
    generado = False

    Randomize

    Do While Not generado
        num = limInf + Rnd * Abs(limSup * 2 - limInf)
        If (num >= limInf And num <= limSup) Then
            generado = True
        End If
        
        cont = cont + 1
    
        If cont > 100 Then    ' en caso que despues de 100 iteraciones no
            num = limInf      ' se haya generado un numero en el tramo
            generado = True
        End If
    
    Loop

    generarNumAleatorio = num

End Function


Private Sub cmdDetener_Click()

    Call DetenerRelojes
            
End Sub

Private Sub Reloj_Timer(Index As Integer)

    Dim num As Integer
    Dim columna As Integer
    Dim sum As Integer
    
    columna = generarNumAleatorio(1, 3)
    num = generarNumAleatorio(-10, 10)
    Me.MSFlexGrid1.TextMatrix(Index + 1, columna) = Str(num)

    sum = calcularSumas

    If (sum <> -1) Then
        
        Me.Figura.FillColor = vbGreen
        Me.MSFlexGrid1.TextMatrix(4, 4) = Str(sum)
    Else
        Call DetenerRelojes
        Me.Figura.FillColor = vbRed
        Me.MSFlexGrid1.TextMatrix(4, 4) = "SUM DIF"
    End If


End Sub

Private Function calcularSumas() As Integer

    Dim sumFila(1 To 3) As Integer
    Dim sumCol(1 To 3) As Integer
    Dim i As Integer, j As Integer
    Dim sc As Integer, sf As Integer
    
    For i = 1 To 3
        sumFila(i) = 0
        For j = 1 To 3
            sumFila(i) = sumFila(i) + CInt(Me.MSFlexGrid1.TextMatrix(i, j))
        Next j
    Next i
            
    For j = 1 To 3
        sumCol(j) = 0
        For i = 1 To 3
            sumCol(j) = sumCol(j) + CInt(Me.MSFlexGrid1.TextMatrix(i, j))
        Next i
    Next j
            
            
    For i = 1 To 3
        Me.MSFlexGrid1.TextMatrix(4, i) = Str(sumCol(i))
        sf = sf + sumFila(i)
        Me.MSFlexGrid1.TextMatrix(i, 4) = Str(sumFila(i))
        sc = sc + sumCol(i)
    Next i
            
    If sc <> sf Then
        calcularSumas = -1
    Else
        calcularSumas = sf
    End If
              
End Function

Private Sub DetenerRelojes()
    Dim i As Integer

    For i = 0 To 2
        Me.Reloj(i).Enabled = Not Me.Reloj(i).Enabled
    Next i
End Sub



4) Implementar un programa que permita asociar en una tabla, Continentes, paises y capitales. usando MSFlexGrid y ComboBox.
Los elementos del ComboBox, deben ser cargados desde los archivos de texto Continentes.txt y PaisCapital.txt.




Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1 
   Caption         =   "Formulario de asociacion "
   ClientHeight    =   4410
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   8295
   LinkTopic       =   "Form1"
   ScaleHeight     =   4410
   ScaleWidth      =   8295
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdAsociar 
      Caption         =   "Asociar"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   12
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   495
      Left            =   720
      TabIndex        =   4
      Top             =   3480
      Width           =   2415
   End
   Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1 
      Height          =   3255
      Left            =   3840
      TabIndex        =   3
      Top             =   360
      Width           =   4095
      _ExtentX        =   7223
      _ExtentY        =   5741
      _Version        =   393216
      Rows            =   10
      Cols            =   4
      MergeCells      =   1
      AllowUserResizing=   1
      FormatString    =   ""
   End
   Begin VB.ComboBox Continente 
      Height          =   315
      Left            =   720
      Sorted          =   -1  'True
      Style           =   2  'Dropdown List
      TabIndex        =   2
      Top             =   600
      Width           =   2775
   End
   Begin VB.ComboBox Capital 
      Height          =   315
      Left            =   720
      Sorted          =   -1  'True
      Style           =   2  'Dropdown List
      TabIndex        =   1
      Top             =   2640
      Width           =   2775
   End
   Begin VB.ComboBox Pais 
      Height          =   315
      Left            =   720
      Sorted          =   -1  'True
      Style           =   2  'Dropdown List
      TabIndex        =   0
      Top             =   1680
      Width           =   2775
   End
   Begin VB.Label lblEntidad 
      Caption         =   "Capital"
      Height          =   255
      Index           =   2
      Left            =   720
      TabIndex        =   7
      Top             =   2400
      Width           =   2295
   End
   Begin VB.Label lblEntidad 
      Caption         =   "Pais"
      Height          =   255
      Index           =   1
      Left            =   720
      TabIndex        =   6
      Top             =   1440
      Width           =   2295
   End
   Begin VB.Label lblEntidad 
      Caption         =   "Continente"
      Height          =   255
      Index           =   0
      Left            =   720
      TabIndex        =   5
      Top             =   360
      Width           =   2295
   End
End
Private Sub cmdAsociar_Click()

    With Me.MSFlexGrid1
        .TextMatrix(.Row, 1) = Continente.Text
        .TextMatrix(.Row, 2) = Pais.Text
        .TextMatrix(.Row, 3) = Capital.Text
        If .Rows > .Row + 1 Then
            .Row = .Row + 1
        Else
            MsgBox "Ahora solo se pueden realizar asociaciones sobre una fila existente" & _
            vbCrLf & "Para ello seleccione la fila con el mouse"
        End If
    End With

End Sub

Private Sub Form_Load()

    Call CargarContinentes
    Call cargarPaisCapital
    
    Me.MSFlexGrid1.TextMatrix(0, 1) = "Continente"
    Me.MSFlexGrid1.TextMatrix(0, 2) = "Pais"
    Me.MSFlexGrid1.TextMatrix(0, 3) = "Capital"
        
End Sub

Private Sub CargarContinentes()

    Dim elemento As String
    On Error GoTo Manejador_Error
    
    Open "Continentes.txt" For Input As #1
    Do Until EOF(1)
        Line Input #1, elemento
        Continente.AddItem elemento
    Loop
    Close #1
    
    Continente.ListIndex = 0
    
    Exit Sub
Manejador_Error:
    MsgBox "No se pudo cargar los datos a la lista continente"


End Sub

Private Sub cargarPaisCapital()

    Dim elemento As String
    On Error GoTo Manejador_Error
    
    Open "PaisCapital.txt" For Input As #1
    Do Until EOF(1)
        Line Input #1, elemento
        Pais.AddItem elemento
        
        Line Input #1, elemento
        Capital.AddItem elemento
    Loop
    Close #1
    
    Pais.ListIndex = 0
    Capital.ListIndex = 0
    
    Exit Sub
Manejador_Error:
    MsgBox "No se pudo cargar los datos a la lista continente"

End Sub