Coding4ever’s Blog

Just coding… coding… and coding… because coding should be fun :)

Coding4Ever Advanced TextBox

| Comments

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 :D) bisa menghemat kode yang enggak berguna  sampai 10-20 %.

Untuk saat ini fitur tambahannya masih minim sih, tapi…. lumayan lah :D

  1. Conversion ada 2 pilihan Normal dan UpperCase, jika dipilih UpperCase otomatis input menggunakan huruf besar.

  2. Numeric Only

  3. Letter Only

  4. Thousand Separator/pemisah ribuan, untuk fitur ini masih ada kekurangan yaitu belum mendukung digit decimal.

  5. Auto Enter, agar fitur ini berfungsi dengan baik maka harus diatur terlebih dulu properties TabIndexnya

  6. EnterFocusColor

  7. LeaveFocusColor

  8. SelectionText

Oke kita lihat dulu demonya

klo udah kita masuk ke bagian dalamnya

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
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

Referensi : Extended TextBox Component Class - VB.Net

Selamat MENCOBA :)

visual basic .net

Tentang Penulis

Software developer yang fokus mengembangkan aplikasi di atas platform .NET (Desktop, ASP.NET MVC, Web Service, Microservice) dan Android. Senang mempelajari teknologi baru terutama di bidang OOP, Design Pattern, ORM, Database, Continuous Integration & Deployment dan arsitektur Microservice.
Selain mengajar, saat ini penulis juga bekerja sebagai staf IT di salah satu PTS di Yogyakarta sebagai senior software developer. Di waktu luang insya Alloh akan terus berbagi pengalaman di blog ini :)

« Tutorial Active Report bagian 1 Backup dan Restore Database Firebird »

Comments