VB.NET: MaskedTextBox Control


MaskedTextBox Control serupa dengan TextBox control namun dengan tambahan fungsi membatasi input user dengan format tertentu.

Contoh Penggunaan:
Design UI
Code
Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Me.Load

    Button1.Text = "Jumlah!"
    MaskedTextBox1.Mask = "0000000"
    MaskedTextBox2.Mask = "0000000"

End Sub
 

Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click


    MaskedTextBox1.Mask = "0000000"
    MaskedTextBox2.Mask = "0000000"

 
    Dim a As Integer = Val(MaskedTextBox1.Text)
   
Dim b As Integer = Val(MaskedTextBox2.Text)
   
Dim c As Integer = Val(MaskedTextBox1.Text) + Val(MaskedTextBox2.Text)
 
    Dim str As String = CInt(c)

    If Len(str) = 1 Then
        MaskedTextBox3.Mask = "0"
    ElseIf Len(str) = 2 Then
        MaskedTextBox3.Mask = "00"
   
ElseIf Len(str) = 3 Then
        MaskedTextBox3.Mask = "0,00"
   
ElseIf Len(str) = 4 Then
        MaskedTextBox3.Mask = "0,000"
   
ElseIf Len(str) = 5 Then
        MaskedTextBox3.Mask = "00,000"
   
ElseIf Len(str) = 6 Then
        MaskedTextBox3.Mask = "0,00,000"
   
ElseIf Len(str) = 7 Then
        MaskedTextBox3.Mask = "00,00,000"
    End If
    MaskedTextBox3.Text = c


End Sub

Runtime



Click here if you like this article.


Post a Comment

0 Comments