Kamis, 01 Maret 2012

Default Textbox Customized (Numeric Only)

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim isKey As Boolean = e.KeyChar.IsDigit(e.KeyChar)
Dim isDecimal As Boolean = e.KeyChar.ToString = "."
Dim isBackSpace As Boolean = e.KeyChar.ToString = " "

If Not isKey And Not isDecimal And Not isBackSpace Then
e.Handled = True
End If
End Sub

Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Dim i, ctr As Integer
Dim temp As String
TextBox1.BackColor = Color.White
temp = ""
For i = Len(TextBox1.Text) To 1 Step -1
ctr += 1
If ctr Mod 3 = 0 And i <> 1 Then
temp = "." & Mid(TextBox1.Text, i, 1) & temp
Else
temp = Mid(TextBox1.Text, i, 1) & temp
End If
Next
TextBox1.Text = temp
End Sub

Private Sub TextBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Dim i As Integer
Dim temp As String
TextBox1.BackColor = Color.LightCyan
temp = ""
For i = 1 To Len(TextBox1.Text)
If IsNumeric(Mid(TextBox1.Text, i, 1)) Then
temp = temp & Mid(TextBox1.Text, i, 1)
End If
Next
TextBox1.Text = temp
End Sub