Help needed for restricted textbox code (Specifically, building a class library with code)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I recently found this code (provided for third party use on another VB site), however, all of my attempts to insert it into a class library have failed.

I open a new class library and past the code in, and immediately get several errors pertaining to how certain objects cant be found. I find it it is crucial to use this code, unless someone can suggest to me another example of existing code that will do
the same thing: make a restricted textbox who imputs can be restricted, that can handle pasting, shortcuts, text property setting, and script-entered text.


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Option Strict <span style="color:Blue; On

<span style="color:Blue; Imports System.ComponentModel

<span style="color:Blue; Public <span style="color:Blue; Class RestrictedTextBox
<span style="color:Blue; Inherits TextBox

<span style="color:Green; Tracks the allowed control content
<span style="color:Blue; Private _allowedContent <span style="color:Blue; As AllowedContentTypes = AllowedContentTypes.<span style="color:Blue; Any

<span style="color:Green; Event to raise when invalid content is attempted
<span style="color:Blue; Public <span style="color:Blue; Delegate <span style="color:Blue; Sub InvalidKeystrokeEventHandler(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As RestrictedTextBoxEventArgs)
<span style="color:Blue; Public <span style="color:Blue; Event InvalidKeystroke <span style="color:Blue; As InvalidKeystrokeEventHandler

<span style="color:Green; Enumeration of valid content types, use the Flags() attribute
<span style="color:Green; as this allows combinations to be defined.
<Flags()> _
<span style="color:Blue; Public <span style="color:Blue; Enum AllowedContentTypes
Alphabetic = 1
Numeric = 2
Alphanumeric = 3
Punctuation = 4
PunctuatedAlphabetic = 5
Separators = 8
Symbols = 16
<span style="color:Blue; Any = 31
AllowCustom = 32
DenyCustom = 64
<span style="color:Blue; End <span style="color:Blue; Enum

<span style="color:Blue; Private _customCharacters <span style="color:Blue; As <span style="color:Blue; String

<Description(<span style="color:#A31515; "Valid list of characters the control can contain, used when allowed content types is set to custom")> _
<span style="color:Blue; Public <span style="color:Blue; Property CustomCharacters <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _customCharacters
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_customCharacters = value
<span style="color:Blue; End <span style="color:Blue; Set

<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Green; IDE Stuff
<Description(<span style="color:#A31515; "Defines the type of content the control can contain"), _
Category(<span style="color:#A31515; "Behavior"), DefaultValue(<span style="color:Blue; GetType(AllowedContentTypes), <span style="color:#A31515; "Any")> _
<span style="color:Blue; Public <span style="color:Blue; Property AllowedContent() <span style="color:Blue; As AllowedContentTypes
<span style="color:Blue; Get
<span style="color:Blue; Return _allowedContent
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal Value <span style="color:Blue; As AllowedContentTypes)
_allowedContent = Value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Green; Handle the event raising in one place...
<span style="color:Blue; Protected <span style="color:Blue; Sub OnInvalidKeystroke(<span style="color:Blue; ByVal e <span style="color:Blue; As RestrictedTextBoxEventArgs)
<span style="color:Blue; RaiseEvent InvalidKeystroke(<span style="color:Blue; Me, e)
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Green; when a key is pressed check if it should be allowed or not.
<span style="color:Blue; Private <span style="color:Blue; Sub RestrictedTextBox_KeyPress(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.Windows.Forms.KeyPressEventArgs) <span style="color:Blue; Handles <span style="color:Blue; MyBase.KeyPress

<span style="color:Green; Dont interfere with control codes (ctrl+v etc)
<span style="color:Blue; If <span style="color:Blue; Char.IsControl(e.KeyChar) <span style="color:Blue; Then <span style="color:Blue; Return

<span style="color:Green; Is this a valid character for the given content types
<span style="color:Blue; If ValidateCharacter(e.KeyChar) <span style="color:Blue; Then <span style="color:Blue; Return

<span style="color:Green; If keystroke hasnt been approved above then raise our event and
<span style="color:Green; prevent further processing of the keystroke.
OnInvalidKeystroke(<span style="color:Blue; New RestrictedTextBoxEventArgs(e.KeyChar, _allowedContent))
e.Handled = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; Private <span style="color:Blue; Function ValidateCharacter(<span style="color:Blue; ByVal c <span style="color:Blue; As <span style="color:Blue; Char) <span style="color:Blue; As <span style="color:Blue; Boolean
<span style="color:Green; Flags behaves oddly with VB as it doesnt have different boolean and
<span style="color:Green; bitwise operators so we need to convert the enum to an integer...
<span style="color:Blue; Dim ac <span style="color:Blue; As <span style="color:Blue; Integer = Convert.ToInt32(_AllowedContent)

<span style="color:Green; Possibly not the most optimal way to do this but clearer than a large OrElse list.
<span style="color:Green; If this does appear to be a performance issue then it can be altered
<span style="color:Green; Also just relies on the built in framework to determine character type - this means
<span style="color:Green; is should also be region aware.
<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.AllowCustom) > 0 <span style="color:Blue; AndAlso _allowedCharacters.Contains(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.DenyCustom) > 0 <span style="color:Blue; AndAlso <span style="color:Blue; Not _allowedCharacters.Contains(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True

<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.Numeric) > 0 <span style="color:Blue; AndAlso <span style="color:Blue; Char.IsNumber(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.Alphabetic) > 0 <span style="color:Blue; AndAlso <span style="color:Blue; Char.IsLetter(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.Punctuation) > 0 <span style="color:Blue; AndAlso <span style="color:Blue; Char.IsPunctuation(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.Separators) > 0 <span style="color:Blue; AndAlso <span style="color:Blue; Char.IsSeparator(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; If (ac <span style="color:Blue; And AllowedContentTypes.Symbols) > 0 <span style="color:Blue; AndAlso <span style="color:Blue; Char.IsSymbol(c) <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; True

<span style="color:Blue; Return <span style="color:Blue; False
<span style="color:Blue; End <span style="color:Blue; Function

<span style="color:Green; Need to override the Text property, Get is fine but we have the possibility
<span style="color:Green; of people trying to assign invalid text via code so alter the Set part.
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Property Text() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return <span style="color:Blue; MyBase.Text
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
<span style="color:Blue; If ValidateString(value) <span style="color:Blue; Then
<span style="color:Blue; MyBase.Text = value
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Green; Really lame function to check the validity of a string, simply loops through
<span style="color:Green; each character till it finds an invalid one or reaches the end...
<span style="color:Blue; Private <span style="color:Blue; Function ValidateString(<span style="color:Blue; ByVal data <span style="color:Blue; As <span style="color:Blue; String) <span style="color:Blue; As <span style="color:Blue; Boolean

<span style="color:Blue; For <span style="color:Blue; Each c <span style="color:Blue; As <span style="color:Blue; Char <span style="color:Blue; In data
<span style="color:Blue; If ValidateCharacter(c) = <span style="color:Blue; False <span style="color:Blue; Then <span style="color:Blue; Return <span style="color:Blue; False
<span style="color:Blue; Next

<span style="color:Blue; Return <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; Function

<span style="color:Green; Another possible issue is people using Paste to enter invalid text, so we
<span style="color:Green; trap the relevant windows message and handle accordingly
<span style="color:Blue; Protected <span style="color:Blue; Overrides <span style="color:Blue; Sub WndProc(<span style="color:Blue; ByRef m <span style="color:Blue; As System.Windows.Forms.Message)

<span style="color:Blue; Const WM_PASTE <span style="color:Blue; As <span style="color:Blue; Integer = &H302

<span style="color:Blue; Select <span style="color:Blue; Case m.Msg
<span style="color:Blue; Case WM_PASTE
<span style="color:Green; Only interested if there is text based data so check that 1st
<span style="color:Blue; If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text, <span style="color:Blue; True) <span style="color:Blue; Then
<span style="color:Green; If it is text, retrieve it and validate it
<span style="color:Blue; Dim tmp <span style="color:Blue; As <span style="color:Blue; String = Clipboard.GetDataObject.GetData(DataFormats.Text, <span style="color:Blue; True).ToString

<span style="color:Green; If valid perform the paste
<span style="color:Blue; If ValidateString(tmp) <span style="color:Blue; Then
<span style="color:Blue; MyBase.WndProc(m)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Case <span style="color:Blue; Else
<span style="color:Green; All other messages get dealt with by the default handler...
<span style="color:Blue; MyBase.WndProc(m)
<span style="color:Blue; End <span style="color:Blue; Select

<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class

<span style="color:Green; EventArgs clas for InvalidKeystroke event
<span style="color:Blue; Public <span style="color:Blue; Class RestrictedTextBoxEventArgs
<span style="color:Blue; Inherits EventArgs

<span style="color:Blue; Private _char <span style="color:Blue; As <span style="color:Blue; Char
<span style="color:Blue; Private _allowedContent <span style="color:Blue; As RestrictedTextBox.AllowedContentTypes

<span style="color:Green; Allow the handler to retreive the invalid character
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property InvalidCharacter() <span style="color:Blue; As <span style="color:Blue; Char
<span style="color:Blue; Get
<span style="color:Blue; Return _char
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Green; Return the allowed content types.
<span style="color:Blue; Public <span style="color:Blue; ReadOnly <span style="color:Blue; Property AllowedContent() <span style="color:Blue; As RestrictedTextBox.AllowedContentTypes
<span style="color:Blue; Get
<span style="color:Blue; Return _allowedContent
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Green; constructor
<span style="color:Blue; Public <span style="color:Blue; Sub <span style="color:Blue; New(<span style="color:Blue; ByVal invalidCharacter <span style="color:Blue; As <span style="color:Blue; Char, <span style="color:Blue; ByVal allowedContent <span style="color:Blue; As RestrictedTextBox.AllowedContentTypes)
_char = invalidCharacter
_allowedContent = allowedContent
<span style="color:Blue; End <span style="color:Blue; Sub

<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
<br/>


View the full article
 

Similar threads

A
Replies
0
Views
158
ANIL AYDINALP
A
D
Replies
0
Views
97
Donald Uko
D
R
Replies
0
Views
85
Rosemerysalvedora
R
Back
Top