Calculando Notas e Media de um Aluno (com criação de componetes)
Março 12, 2008 de elzobrito
Olá Pessoal,
Estou um pouco sem tempo de comentar agora, mas está aí aquele exemplo de calcular médias e faltas de um aluno, porém nesse novo exemplo todos os controles do Formulário são criados dinamicamente.
Public obj As Object
Public media, falta As Double
Public rp As String
Public limite, faltas As Integer
Public WithEvents cmd_calcula As CommandButton
Public WithEvents txt_nota1 As TextBox
Public WithEvents txt_nota2 As TextBox
Public WithEvents txt_nota3 As TextBox
Public WithEvents txt_nota4 As TextBox
Public WithEvents txt_limite As TextBox
Public WithEvents txt_faltas As TextBox
Public WithEvents lbl_notas As Label
Public WithEvents lbl_limites As Label
Public Sub renomeia()
With Me .lbl_notas.Caption = "nota1 nota2 nota3 nota4"
.lbl_notas.Top = 80
.lbl_notas.Left = 100
.lbl_notas.Visible = True
.lbl_notas.AutoSize = True.lbl_limites.Caption = "Faltas do aluno Limite de Faltas"
.lbl_limites.Top = 1000
.lbl_limites.Left = 100
.lbl_limites.Visible = True
.lbl_limites.AutoSize = True
.txt_nota1.Visible = True
.txt_nota1.Top = 280
.txt_nota1.Width = 500
.txt_nota1.Height = 200
.txt_nota2.Visible = True
.txt_nota2.Top = .txt_nota1.Top
.txt_nota2.Left = .txt_nota1.Left + .txt_nota2.Height + 500
.txt_nota2.Width = .txt_nota1.Width
.txt_nota2.Height = 200
.txt_nota3.Visible = True
.txt_nota3.Top = .txt_nota2.Top
.txt_nota3.Left = .txt_nota2.Left + .txt_nota3.Height + 500
.txt_nota3.Width = .txt_nota2.Width
.txt_nota3.Height = 200
.txt_nota4.Visible = True
.txt_nota4.Top = .txt_nota3.Top
.txt_nota4.Left = .txt_nota3.Left + .txt_nota4.Height + 300
.txt_nota4.Width = .txt_nota3.Width
.txt_nota4.Height = 200
.txt_faltas.Visible = True
.txt_faltas.Top = 1200
.txt_faltas.Width = .txt_nota3.Width
.txt_faltas.Height = 200
.txt_limite.Visible = True
.txt_limite.Top = .txt_faltas.Top
.txt_limite.Left = .txt_faltas.Left + .txt_limite.Height + 1200
.txt_limite.Width = .txt_nota3.Width
.txt_limite.Height = 200
.cmd_calcula.Caption = “Calcular”
.cmd_calcula.Visible = True
.cmd_calcula.Top = 2000
.cmd_calcula.Left = 2000
.Caption = “Controle de Faltas”
End With
End Sub
Public Sub limpa_textos()
For Each obj In Form1.Controls
If TypeOf obj Is TextBox Then
obj.Text = “”
End If
Next
End Sub
Public Sub apaga_menor_num()
If Val(Me.txt_nota1.Text) < Val(Me.txt_nota2.Text) And Val(Me.txt_nota1.Text) < Val(Me.txt_nota3.Text) And Val(Me.txt_nota1.Text) < Val(Me.txt_nota4.Text) Then
Me.txt_nota1.Text = “”
Else
If Val(Me.txt_nota2.Text) < Val(Me.txt_nota1.Text) And Val(Me.txt_nota2.Text) < Val(Me.txt_nota3.Text) And Val(Me.txt_nota2.Text) < Val(Me.txt_nota4.Text) Then
Me.txt_nota2.Text = “”
Else
If Val(Me.txt_nota3.Text) < Val(Me.txt_nota1.Text) And Val(Me.txt_nota3.Text) < Val(Me.txt_nota2.Text) And Val(Me.txt_nota3.Text) < Val(Me.txt_nota4.Text) Then
Me.txt_nota3.Text = “”
Else
If Val(Me.txt_nota4.Text) < Val(Me.txt_nota1.Text) And Val(Me.txt_nota4.Text) < Val(Me.txt_nota2.Text) And Val(Me.txt_nota4.Text) 6 Then
rp = “APROVADO”
End If
If rp = “” Then
If faltas >= limite Then
rp = “REPROVADO POR NOTA E POR FALTA”
Else
rp = “REPROVADO POR NOTA”
End If
End If
MsgBox rp, vbCritical, “Controle de Faltas”
End Sub
Private Sub Form_Load()
Set txt_nota1 = Me.Controls.Add(”VB.TextBox”, “txt_nota1″)
Set txt_nota2 = Me.Controls.Add(”VB.TextBox”, “txt_nota2″)
Set txt_nota3 = Me.Controls.Add(”VB.TextBox”, “txt_nota3″)
Set txt_nota4 = Me.Controls.Add(”VB.TextBox”, “txt_nota4″)
Set txt_faltas = Me.Controls.Add(”VB.TextBox”, “txt_faltas”)
Set txt_limite = Me.Controls.Add(”VB.TextBox”, “txt_limite”)
Set lbl_limites = Me.Controls.Add(”VB.Label”, “lbl_limites”)
Set lbl_notas = Me.Controls.Add(”VB.Label”, “lbl_notas”)
Set cmd_calcula = Me.Controls.Add(”VB.CommandButton”, “cmd_calcula”)
Me.limpa_textos
Me.renomeia
End Sub