Kamis, 25 Desember 2014

Validasi Penulisan Angka Desimal pada Textbox dengan VB.NET (Part 2)

Pada posting saya sebelumnya, saya ada menulis tentang cara membuat textbox agar hanya bisa menerima input angka saja dan huruf pemisah desimal (koma). Untuk huruf pemisah desimal, hanya bisa diinput 1 kali saja. Validasi tersebut sudah cukup bagus. Namun masih ada kekurangan yakni textbox tidak bisa menerima input huruf - (minus / negatif). Untuk menjawab kekurangan tersebut, pada posting ini saya akan menyempurnakan source code sebelumnya sehingga textbox hanya hanya bisa menerima input angka, pemisah desimal dan tombol negatif saja. Jika pada source code sebelumnya ditulis dengan statement IF, maka pada source code ini saya ubah dengan statement Select Case. Hasilnya tetap sama. Penulisan source code juga masih dilakukan di event KeyPress textbox. Untuk textbox tersebut, saya set property TextAlign = Right dan MaxLength = 20 pada event Form Load-nya.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TextBox1.TextAlign = HorizontalAlignment.Right
    Me.TextBox1.MaxLength = 20
End Sub
Berikut source pada event KeyPress textbox tersebut :
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    Select Case e.KeyChar
        Case CChar(vbBack) 'JIKA TOMBOL BACKSPACE
            e.Handled = False 'LANJUT INPUT
        Case "0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c 'JIKA TOMBOL 0-9
            e.Handled = False
        Case "-"c 'JIKA TOMBOL - (MINUS)
            If Me.TextBox1.Text.Contains("-"c) = True Then 'JIKA TEXTBOX SUDAH PUNYA HURUF MINUS
                e.Handled = True 'STOP INPUT
            Else 'JIKA TEXTBOX BELUM ADA HURUF MINUS
                If Me.TextBox1.Text = String.Empty Then 'JIKA TEXTBOX MASIH KOSONG
                    e.Handled = False 'LANJUT INPUT HURUF MINUS
                Else 'JIKA BELUM ADA HURUF MINUS TAPI SUDAH ADA ANGKA
                    e.Handled = True 'STOP INPUT HURUF MINUS
                End If
            End If
        Case CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator) 'JIKA TOMBOL PEMISAH DESIMAL
            'JIKA SUDAH ADA HURUF PEMISAH DESIMAL
            If Me.TextBox1.Text.Contains(CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)) = True Then
                e.Handled = True 'STOP INPUT
            Else 'JIKA BELUM ADA HURUF PEMISAH DESIMAL
                If Me.TextBox1.Text = String.Empty Then 'JIKA TEXTBOX MASIH KOSONG
                    'TAMBAHKAN O (NOL) DIDEPAN HURUF PEMISAH DESIMAL
                    Me.TextBox1.Text = "0"c & CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
                    'STOP INPUT
                    e.Handled = True
                    'PINDAHKAN CURSOR DITEXTBOX KE BELAKANG TEXT
                    Me.TextBox1.Select(Me.TextBox1.Text.Length, 0)
                Else 'JIKA BELUM ADA HURUF PEMISAH DESIMAL TAPI SUDAH ADA ANGKA
                    e.Handled = False 'LANJUT INPUT
                End If
            End If
        Case CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator) 'JIKA TOMBOL PEMISAH RIBUAN
            'JIKA SUDAH ADA HURUF PEMISAH DESIMAL
            If Me.TextBox1.Text.Contains(CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)) = True Then
                e.Handled = True 'STOP INPUT
            Else 'JIKA BELUM ADA HURUF PEMISAH DESIMAL
                If Me.TextBox1.Text = String.Empty Then 'JIKA TEXTBOX MASIH KOSONG
                    'TAMBAHKAN O (NOL) DIDEPAN HURUF PEMISAH DESIMAL
                    Me.TextBox1.Text = "0"c & CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator)
                    'STOP INPUT
                    e.Handled = True
                    'PINDAHKAN CURSOR DITEXTBOX KE BELAKANG TEXT
                    Me.TextBox1.Select(Me.TextBox1.Text.Length, 0)
                Else 'JIKA BELUM ADA HURUF PEMISAH DESIMAL TAPI SUDAH ADA ANGKA
                    e.KeyChar = CChar(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator) 'LANJUT INPUT TAPI HURUFNYA DIUBAH
                End If
            End If
        Case Else 'JIKA BUKA TOMBOL BACKSPACE, 0-9, -, PEMISAH DESIMAL, PEMISAH RIBUAN
            e.Handled = True 'STOP INPUT
    End Select
End Sub
Untuk mencoba source code diatas, kita cukup membuat project baru yang bertipe Windows Forms Application di visual studio versi apapun. (2005, 2008, 2010, 2012 atau 2013). Tentu bahasa pemograman yang digunakan adalah VB.NET. Setelah dibuat, maka akan muncul 1 form kosong di project tersebut. Kemudian tambahkan 1 buat textbox ke form tersebut. Setelah itu, tampilkan source code dengan cara tekan F7. Di tampilan source code, paste-kan source code diatas. Setelah itu, anda cukup menjalankan program tersebut (tekan F5) untuk mencoba programnya. 
Silahkan dicoba dan semoga bermanfaat. Jika ada error atau pertanyaan, silahkan beri komentar anda pada bagian bawah berikut.

Tidak ada komentar:

Posting Komentar