'____________________________________________________________________________________ ' R K T E X T B O X C U S T O M C O N T R O L ' Version 1.0 ' By Ron Kessler ' Created 12/15/06 '____________________________________________________________________________________ ' 'Updated 12/26/06 'This custom textbox control is based upon the Windows textbox control. But I added some stuff! '1. This control has a DataType property that lets you limit/filter what they type in. ' They can type All (no filtering) ' They can choose letters only or numbers only. '2 The backspace and decimal and space bar keys are allowed also. '3 The TrimSpaces property, when set to true, will erase leading/trailing spaces using the Trim function. '4 There is one method called OnKeyPress which Overrides the MS method that comes with the Textbox ' control. This way, I can make it the way I want to but still stick with the same name so programmers ' will understand how it works! I use the ConsoleKey and ControlChars enumerations to trap keystrokes. 'PROGRAMMING NOTES: '1 You cannot test your control and build it in the same solution. I start two instances of VS. One to build/modify ' the control as I go along and the other to use as a demo or test project. '2 To build a custom control you create a new Windows Control Library project. If you want, you can use ' any controls you want and bundle them as a new control. But I started from scratch and there is no user ' interface for my control. You will only see the code for it. '3 Be sure to set any initial property values in the .Designer.vb file in you solution. Look under the section called ' "Private Sub InitializeComponent()" and place it there. It will not initialize if you put it in the control's project itself. '4 Finally, you can make changes to your control, rebuild, then switch to the demo program and all the new changes ' work automatically! '5 When you build your app, choose Clean Solution and Clean Project from the build menu first. Then choose ' Re-build solution. This deletes old assembly .DLL's and starts fresh. '6 When you add your control to a demo project, be sure to use the version inside the \bin\release folder! Public Class RKTextBox Inherits System.Windows.Forms.TextBox Private m_LegalKeys As KeystrokeOption 'holds the selected datatype Private m_TrimSpaces As Boolean 'do they want to trim spaces? '---define the values for the legal keys in an enumeration _ ' This is like creating a variable but limiting the values to those in my list. Enum KeystrokeOption All LettersOnly NumbersOnly End Enum ''' ''' Allows textbox to be filtered so letters, numbers, or both can be entered. ''' ''' ''' ''' Public Property DataType() As KeystrokeOption Get DataType = m_LegalKeys End Get Set(ByVal Value As KeystrokeOption) m_LegalKeys = Value End Set End Property ''' ''' Allows user to trim all leading/trailing spaces from text. ''' ''' ''' ''' Public Property TrimSpaces() As Boolean Get TrimSpaces = m_TrimSpaces End Get Set(ByVal Value As Boolean) m_TrimSpaces = Value If m_TrimSpaces = True Then Me.Text = Trim(Me.Text) End If End Set End Property ''' ''' Method filters the keystrokes based upon datatype property enumeration. ''' ''' ''' Protected Overrides Sub OnKeypress(ByVal e As System.Windows.Forms.KeyPressEventArgs) '---protected modifier allows sub to be used in classes derived from this one and be used in ' the assembly of this project but not other assemblies Select Case m_LegalKeys Case KeystrokeOption.All 'allow any keystroke e.Handled = False 'don't block Case KeystrokeOption.LettersOnly 'lower or upper case letters & backspace key If UCase(e.KeyChar) >= "A" And UCase(e.KeyChar) <= "Z" _ Or e.KeyChar = ControlChars.Back _ Or ConsoleKey.Spacebar Then 'space e.Handled = False Else e.Handled = True 'block it End If Case KeystrokeOption.NumbersOnly 'numbers only & "." & Backspace key If (e.KeyChar >= "0" And e.KeyChar <= "9") _ Or e.KeyChar = "." _ Or e.KeyChar = ControlChars.Back Then e.Handled = False Else e.Handled = True 'block it End If Case Else e.Handled = True End Select End Sub End Class