Delegate Generic Event Handling

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several