Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: All
Aug 4, 2000

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 of code:
 Option Explicit
Dim mobjAutoSelectContent As New 
prjsel.clsSelectContent Private Sub Form_Load() mobjAutoSelectContent.Init Me End Sub
So the coding is reduced dramatically and so is the testing effort. The code behind the component is:
 Option Explicit

Private mobjfrm As Form  'stores the 
form Private mctrlActiveControl As Control
'stores the ActiveControl Private WithEvents mTimer As Timer Public 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 containing form 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 to the 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 Sub Private Sub Class_Terminate() Set mctrlActiveControl = Nothing Set mobjfrm = Nothing End Sub Private 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 in the mctrlActiveControl 'variable, do the following processing Set mctrlActiveControl = mobjfrm.ActiveControl If ((TypeOf mctrlActiveControl Is ComboBox)
Or (TypeOf mctrlActiveControl Is TextBox)) Then mctrlActiveControl.SelStart = 0 mctrlActiveControl.SelLength =
Len(mctrlActiveControl.Text) End If End If End Sub
Jatin Shah
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date