Auto Select Content Component

Auto Select Content Component

We almost always need to develop forms in which the contents to the control have to be selected whenever the user tabs to a textbox or a combobox. For this we write two lines in the GotFocus event of each such control on the form. If the form contains 30 controls, we have to write 60 lines of code for them:

 Private Sub Text1_GotFocus()    Text1.SelStart = 0    Text1.SelLength = Len(Text1.Text)End Sub

But if we use the following component, we have to write just two lines ofcode:

 Option ExplicitDim mobjAutoSelectContent As New 
prjsel.clsSelectContent Private Sub Form_Load() mobjAutoSelectContent.Init MeEnd Sub

So the coding is reduced dramatically and so is the testing effort. The code behind the component is:

 Option ExplicitPrivate mobjfrm As Form  'stores the 
formPrivate mctrlActiveControl As Control
'stores the ActiveControlPrivate WithEvents mTimer As TimerPublic Sub Init(pfrm As Object) If TypeOf pfrm Is Form Then 'if the sent obj is FORM, set
it to the module obj mobjfrm Set mobjfrm = pfrm Else 'If the sent object is not the form,
iterate until the containingform is found Dim lctrl As Object 'set lctrl to the control passed to
the method Set lctrl = pfrm While Not (TypeOf lctrl Is Form) 'loop upwards using .container
prop of the control to reach tothe containing FORM Set lctrl = lctrl.Container Wend Set mobjfrm = lctrl End If 'Create a timer to select the text of the
active control Set mTimer = mobjfrm.Controls.Add("VB.Timer",
"Timer1_SelectContent" &CStr(mobjfrm.Controls.Count)) mTimer.Interval = 100 mTimer.Enabled = True End SubPrivate Sub Class_Terminate() Set mctrlActiveControl = Nothing Set mobjfrm = NothingEnd SubPrivate Sub mTimer_Timer() 'select the text of the active control If Not (mctrlActiveControl Is mobjfrm.ActiveControl)
Then 'Only if the currently Active control is
different than the one inthe mctrlActiveControl 'variable, do the following processing Set mctrlActiveControl = mobjfrm.ActiveControl If ((TypeOf mctrlActiveControl Is ComboBox)
Or (TypeOfmctrlActiveControl Is TextBox)) Then mctrlActiveControl.SelStart = 0 mctrlActiveControl.SelLength =
Len(mctrlActiveControl.Text) End If End IfEnd Sub
Share the Post:
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

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as