web stats

Sabtu, 16 Agustus 2014

Text Box's Rules (Tip) - Visual Basic

Tip ini digunakan untuk mengatur karakter yang harus dimasukkan pada Text Box di Visual Basic. Dalam contoh ini ada 5 kondisi pada Text Box :
1. Text Box harus diisi dengan angka numerik.
2. Text Box harus diisi dengan huruf.
3. Secara otomatis Text Box diisi dengan huruf kecil.
4. Secara otomatis Text Box diisi dengan huruf kapital.
5. Maksimal karakter pada Text Box adalah 8. (bisa diatur sesuai keinginan)
Buka program VB, kemudian buat formasi tool dan propertiesnya sebagai berikut.
Setelah itu, masukkan kode berikut ke dalam kode editor.

Private Sub Text1_Keypress(KeyAscii As Integer)
If Not IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
MsgBox "Please insert number."
Text1.Text = ""
End If
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
If IsNumeric(Chr(KeyAscii)) Then
KeyAscii = 0
MsgBox "Please insert letter."
Text2.Text = ""
End If
End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)
lowercase = Chr(KeyAscii)
KeyAscii = Asc(LCase(lowercase))
If KeyAscii = 13 Then
Text1.Text = "": KeyAscii = 0
End If
End Sub

Private Sub Text4_KeyPress(KeyAscii As Integer)
uppercase = Chr(KeyAscii)
KeyAscii = Asc(UCase(uppercase))
If KeyAscii = 13 Then
Text1.Text = "": KeyAscii = 0
End If
End Sub

Private Sub Text5_KeyPress(KeyAscii As Integer)
If Len(Text5) >= 8 Then
KeyAscii = 0
MsgBox "You already reaches maximum characters."
Text5.Text = ""
End If
End Sub


Catatan : Tulisan berwarna biru bisa diganti dengan karakter apapun.
Setelah itu, jalankan program dengan menekan tombol F5.
Tampilan program setelah mengetikkan karakter di Text Box sesuai dengan "aturan"
OK! Tip telah berhasil dibuat....
Jika ada permasalahan, silahkan komentarnya dibawah..

Tidak ada komentar:

Posting Komentar