Label

Senin, 07 November 2011

Latihan VB6, aplikasi Kartu Flash Soal Penjumlahan (Flash Card Addition Problems)

Disini kita akan membuat aplikasi dengan fungsi random untuk membangkitkan soal penjumlahan. Lalu kita akan memberikan semacam umpan balik dan sistem penilaian setelah soal dijawab. Dalam aplikasi ini terdapat beberapa label/control yang tidak dicantumkan propertinya, silahkan dicoba sendiri propertinya.





Properties:

Form frmAdd:
BorderStyle = 1 - Fixed Single
Caption = Flash Card Addition

CommandButton cmdNext:
Caption = &Next Problem
Enabled = False

CommandButton cmdExit:
Caption = E&xit

TextBox txtAnswer:
FontName = Arial
FontSize = 48
MaxLength = 2

Label lblMessage:
Alignment = 2 - Center
BackColor = &H00FFFF00& (Cyan)
BorderStyle = 1 - Fixed Single
FontName = MS Sans Serif
FontBold = True
FontSize = 24
FontItalic = True

Label lblScore:
Alignment = 2 - Center
BackColor = &H0000FFFF& (Yellow)
BorderStyle = 1 - Fixed Single
Caption = 0
FontName = Times New Roman
FontBold = True
FontSize = 36

Label Label1:
Alignment = 2 - Center
Caption = Score:
FontName = MS Sans Serif
FontSize = 18

Label Label4:
Alignment = 2 - Center
Caption = =
FontName = Arial
FontSize = 4
 
Label lblNum2:
Alignment = 2 - Center
FontName = Arial
FontSize = 48

Label Label2:
Alignment = 2 - Center
Caption = +
FontName = Arial
FontSize = 48

Label lblNum1:
Alignment = 2 - Center
FontName = Arial
FontSize = 48

Code Programnya:
Bagian general:

Option Explicit
Dim Sum As Integer
Dim NumProb As Integer, NumRight As Integer, n As Integer

Untuk code tombol exitnya ini:

Private Sub cmdExit_Click()
End
End Sub

Untuk code tombol next ini:
Private Sub cmdNext_Click()
'Generate next addition problem
Dim Number1 As Integer
Dim Number2 As Integer
txtAnswer.Text = ""
lblMessage.Caption = ""
NumProb = NumProb + 1
'Generate random numbers for addends
Number1 = Int(Rnd * 21)
Number2 = Int(Rnd * 21)
lblNum1.Caption = Format(Number1, "#0")
lblNum2.Caption = Format(Number2, "#0")
'Find sum
Sum = Number1 + Number2
cmdNext.Enabled = False
txtAnswer.SetFocus
End Sub

Tuliskan saja baris kode ini:

Private Sub Form_Activate()
Call cmdNext_Click
End Sub

Private Sub Form_Load()
Randomize Timer
n = 0
NumProb = 0
NumRight = 0
End Sub


Bagian berikutnya, code yang digunakan untuk memilih hanya tombol angka 0-9, backspace dan enter adalah sebagai berikut:

Private Sub txtAnswer_KeyPress(KeyAscii As Integer)
Dim Ans As Integer
'Check for number only input and for return key
If (KeyAscii >= vbKey0 And KeyAscii <= vbKey9) Or KeyAscii = vbKeyBack Then
    Exit Sub
ElseIf KeyAscii = vbKeyReturn Then
'Check answer
    Ans = Val(txtAnswer.Text)
    If Ans = Sum Then
        NumRight = NumRight + 1
        lblMessage.Caption = "Kamu Benar!"
    Else
        lblMessage.Caption = "Jawabannya " + Format(Sum, "#0")
    End If
    lblScore.Caption = Format(100 * NumRight / NumProb, "##0")
    cmdNext.Enabled = True
    cmdNext.SetFocus
    n = n + 1
    lblNomor.Caption = n
Else
    KeyAscii = 0
End If

Semoga jelas dan berguna bagi kita semua.
Selamat belajar VB…:D

Mau sharing kirim e-mail ke indarto.irfan@gmail.com

Tidak ada komentar:

Posting Komentar