Delegate Generic Event Handling

It can be useful to create generic controls of similar properties. For example, if a project has 10 textboxes on different forms that need to accept numeric input only, instead of repeating the same code in every textbox, you can create a class called clsGeneric and declare the control using WithEvents. You can then trap the events in one place.

Create a collection of the clsGeneric class and keep adding controls to the collection. When you end the app, set the Collection object to Nothing. You can use control arrays, but if you’re using them across the forms, you don’t have to repeat the code:

 ' --- Save the following code in clsGeneric.clsOption ExplicitPublic WithEvents txtAny As TextBoxPrivate Sub txtAny_GotFocus()	If Len(Trim$(txtAny)) > 0 Then		txtAny.SelStart = 0		txtAny.SelLength = Len(txtAny)	End IfEnd Sub' -- Save the following code in clsGenerics.clsOption ExplicitPrivate mColGenerics As New CollectionPublic Function Add(ByVal txtAny As TextBox, Optional _	ByVal Key As String = "") As clsGeneric	Dim clsAny As New clsGeneric	Set clsAny.txtAny = txtAny	If Key = "" Then		mColGenerics.Add clsAny	Else		mColGenerics.Add clsAny, Key	End If	Set Add = clsAny	' Return a reference to the new textboxEnd FunctionPublic Function Count() As Long	Count = mColGenerics.CountEnd FunctionPublic Sub Delete(ByVal Index As Variant)	mColGenerics.Remove IndexEnd SubPublic Function Item(ByVal Index As Variant) As clsGeneric	Set Item = mColGenerics.Item(Index)End FunctionPublic Function NewEnum() As IUnknown	Set NewEnum = mColGenerics.[_NewEnum]End Function' -- In any form or global module where you want to have ' -- this generic textboxesPrivate clsTexts As New clsGenerics' In form load add the controls to the collection like this.	clsTexts.Add Text1	clsTexts.Add Text2	clsTexts.Add Text3' You can even declare clsTexts globally and keep adding ' controls in whatever forms needed.
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS