Coding4Ever Advanced TextBox
Project baru kebetulan harus saya kerjakan menggunakan Visual Basic .NET dan untuk menghemat kode-kode yang enggak produktif terutama yang berhubungan dengan proses input (khususnya TextBox) seperti proses enter, validasi huruf/angka, pemisah ribuan, dan perubahan warna objek pada saat focus/lost focus akhirnya saya sempatkan waktu untuk membuat komponen ini.
Jadi dengan menggunakan komponen ini menurut perkiraan saya (berarti masih bisa salah ) bisa menghemat kode yang enggak berguna sampai 10-20 %.
Untuk saat ini fitur tambahannya masih minim sih, tapi…. lumayan lah
- Conversion ada 2 pilihan Normal dan UpperCase, jika dipilih UpperCase otomatis input menggunakan huruf besar.
- Numeric Only
- Letter Only
- Thousand Separator/pemisah ribuan, untuk fitur ini masih ada kekurangan yaitu belum mendukung digit decimal.
- Auto Enter, agar fitur ini berfungsi dengan baik maka harus diatur terlebih dulu properties TabIndexnya
- EnterFocusColor
- LeaveFocusColor
- SelectionText
Oke kita lihat dulu demonya
klo udah kita masuk ke bagian dalamnya
Imports System.Drawing
Imports System.Windows.Forms
<ToolboxBitmap(GetType(AdvancedTextbox), "AdvancedTextbox.bmp")> _
Public Class AdvancedTextbox
Inherits System.Windows.Forms.TextBox
#Region ">> Enumerators <<"
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)> _
Public Enum EConversion
Normal = 0
UpperCase = 1
End Enum
#End Region
#Region ">> Declarations <<"
Private mEnterFocusColor As Color = Color.White
Private mLeaveFocusColor As Color = Color.White
Private mIsSelectionText As Boolean = False
Private mIsThousandSeparator As Boolean = False
Private mIsNumericOnly As Boolean = False
Private mIsLetterOnly As Boolean = False
Private mIsAutoEnter As Boolean = False
Private mIsDecimal As Boolean = False
Private mConversion As EConversion
#End Region
#Region ">> Properties <<"
Public Overrides Property MaxLength() As Integer
Get
Return MyBase.MaxLength
End Get
Set(ByVal value As Integer)
If Me.mIsThousandSeparator AndAlso value > 15 Then value = 15
MyBase.MaxLength = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property EnterFocusColor() As Color
Get
Return mEnterFocusColor
End Get
Set(ByVal value As Color)
mEnterFocusColor = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property LeaveFocusColor() As Color
Get
Return mLeaveFocusColor
End Get
Set(ByVal value As Color)
mLeaveFocusColor = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property SelectionText() As Boolean
Get
Return mIsSelectionText
End Get
Set(ByVal value As Boolean)
mIsSelectionText = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property ThousandSeparator() As Boolean
Get
Return mIsThousandSeparator
End Get
Set(ByVal value As Boolean)
mIsThousandSeparator = Value
If mIsThousandSeparator Then
mIsNumericOnly = True
Me.MaxLength = 15
Me.TextAlign = HorizontalAlignment.Right
Me.Text = "0"
End If
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property NumericOnly() As Boolean
Get
Return mIsNumericOnly
End Get
Set(ByVal value As Boolean)
mIsNumericOnly = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property LetterOnly() As Boolean
Get
Return mIsLetterOnly
End Get
Set(ByVal value As Boolean)
mIsLetterOnly = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property AutoEnter() As Boolean
Get
Return mIsAutoEnter
End Get
Set(ByVal value As Boolean)
mIsAutoEnter = Value
End Set
End Property
<System.ComponentModel.Category("AdvancedTextbox Properties")> _
<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Always)> _
Public Property Conversion() As EConversion
Get
Return mConversion
End Get
Set(ByVal value As EConversion)
mConversion = Value
End Set
End Property
#End Region
#Region ">> Private Function <<"
Private Function ValidasiAngka(ByVal e As System.Windows.Forms.KeyPressEventArgs) As Boolean
Dim strValid As String = "0123456789"
If Not mIsThousandSeparator Then strValid += "."
If Strings.InStr(strValid, e.KeyChar) = 0 And Not (e.KeyChar = Strings.Chr(Keys.Back)) Then
Return True ' not valid
Else
Return False ' valid
End If
End Function ' ValidasiAngka
Private Function ValidasiHuruf(ByVal e As System.Windows.Forms.KeyPressEventArgs) As Boolean
Dim strValid As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. "
If Strings.InStr(strValid, e.KeyChar) = 0 And Not (e.KeyChar = Strings.Chr(Keys.Back)) Then
Return True ' not valid
Else
Return False ' valid
End If
End Function ' ValidasiHuruf
Private Function HurufBesar(ByVal e As System.Windows.Forms.KeyPressEventArgs) As Char
Return CChar(e.KeyChar.ToString().ToUpper())
End Function ' HurufBesar
Private Sub SeleksiText(ByVal sender As System.Windows.Forms.TextBox)
With sender
.SelectionStart = 0
.SelectionLength = .Text.Length
End With
End Sub
#End Region
#Region ">> Control Events <<"
Private Sub AdvancedTextbox_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter
If Me.mIsSelectionText Then Call SeleksiText(CType(sender, System.Windows.Forms.TextBox))
Me.BackColor = Me.mEnterFocusColor
End Sub
Private Sub AdvancedTextbox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If Me.mIsAutoEnter Then If e.KeyChar = Strings.Chr(Keys.Return) Then SendKeys.Send ("{Tab}")
If Me.mIsNumericOnly Then
If mIsDecimal AndAlso e.KeyChar = "." Then
e.Handled = True
Else
e.Handled = ValidasiAngka(e)
End If
ElseIf Me.mIsLetterOnly Then
If Me.mConversion = EConversion.UpperCase Then e.KeyChar = HurufBesar(e)
e.Handled = ValidasiHuruf(e)
ElseIf Me.mConversion = EConversion.UpperCase Then
e.KeyChar = HurufBesar(e)
End If
End Sub
Private Sub AdvancedTextbox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
If Me.mIsNumericOnly Then If Not (Me.Text.Length > 0) Then Me.Text = "0"
Me.BackColor = Me.mLeaveFocusColor
End Sub
Private Sub AdvancedTextbox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
mIsDecimal = False
Dim index As Integer = Me.Text.IndexOf(".")
mIsDecimal = Not (index < 0)
If Me.mIsNumericOnly AndAlso Me.mIsThousandSeparator Then
If Me.Text.Length > 0 Then
If Me.Text.Substring(0, 1) = "." Then Me.Text = Me.Text.Replace(".", "")
Dim x As Long = CLng(Me.Text.Replace(",", ""))
Dim strAfterFormat As String = Strings.FormatNumber(x, 0)
If Me.Text <> strAfterFormat Then
Dim pos As Integer = Me.Text.Length - Me.SelectionStart
Me.Text = strAfterFormat
If ((Me.Text.Length - pos) < 0) Then
Me.SelectionStart = 0
Else
Me.SelectionStart = Me.Text.Length - pos
End If
End If
End If
End If
End Sub
#End Region
End Class
Selamat MENCOBA
Referensi
Comments