Solved Simple textbox class?

Gasman

Enthusiastic Amateur
Local time
Today, 10:49
Joined
Sep 21, 2011
Messages
15,377
This is mainly directed at @MajP as he seems to post the most about classes here, but would welcome input from anyone who creates their own classes.

If I wanted to create a very simple textbox class and let us say I just want to use it to have common code run on GotFocus and LostFocus, so as to identify to the user where they are on a form, what is the minimum code I would need to create in the class.

Would I just then loop through the controls and set the textbox to the textboxclass?

I will be honest, I suggested this method as a way to have common code for all the textboxes, which I have seen happen in some videos, but ther is plenty of other code there, and I want to start small. Then realised whilst I might know it can be done, I do not know exactly how to do it. :(

This is just for my own benefit and understanding, but would also help that member where I suggested this might be a way to do this. That member was on another site.
 

Hi Paul,​

Indeed, MajP has a fast superior knowledge about class modules than most, including myself. I have only played with them once or twice!​

Blog On my website here:-


A very early attempt I made at linking a class module to a control. It might be worth taking a look at. I have no idea if I adopted the correct approach. I cobbled it together from what I saw elsewhere with very little understanding!​


See here:-

Class module Executes Identical Code for "Every" Button​


There are two relevant videos regarding class modules in this post:-

This video shows how to link your command buttons to the class:-




This one demonstrates the class module



I did these videos 13 years ago when I didn't know much about creating YouTube videos. I was a complete to beginner! They are also in an old TV screen size format which YouTube in its infinite wisdom has downgraded in search results so they hardly get any hits these days I need to redo them!
 
Last edited:
Thanks Tony, I will have a look. A lot of examples mention buttons like a navigation class, but this member just wanted an easy way to highlight the current textbox control and not having to code the same code in each control event, even if it was a simple function like =SetBackColour()

That got me a little interested as I have seen some of the AUG presentations. Whilst I have no real need myself, it was something to keep my brain working. :)

Having watched a few videos and walked through some code, that brrain is now hurting. :(
 
I have since found a DB by A.P.R. Pillai that deals just with a textbox control.
He actually combines the class with normal functions for the colouring, which again is a different approach.
 

Attachments

s say I just want to use it to have common code run on GotFocus and LostFocus

Just curious why do you need a class?

Providing the event does not have parameters you need to reference you could have a public function and call it in the event properties

e.g.
Public function txtGotFocus()
With screen.activecontrol
‘Do something
End with
end function

and then select all the texboxes and put

=txtgotfocus()

against the got focus event
where you normally see [event procedure]
 
Yes, but I believe the member did not want to even do that :(

So if they add another control, they have to remember to set those events to that function.

With even a simple class that would happen automatically.
A little lazy perhaps, but just the start of getting my head around classes.
 
In cases like this I make two classes, just like Access does.
If you have a Control, you have a Controls collections (Form, Forms; Field, Fields....)
This make it very easy to then manage the multiple instances of the class.
 
Lets call your New class
LocateableTextBox
and the collection
LocateableTextBoxes

To build the LocateableTextBox class follow the steps here

I will try to demo.
 
Lets call your New class
LocateableTextBox
and the collection
LocateableTextBoxes

To build the LocateableTextBox class follow the steps here

I will try to demo.
I actually read that thread and again it was more than I wanted.
I think I have got it now from the DB A.P.R. Pillai created, at least enough to do what was being asked, so I do not want to trouble you any more, but thank you for offering a demo.
 
Not sure what you want to do when a control has or loses focus. I pass in label to the class and have it print to there
Code:
Option Compare Database
Option Explicit
'------------------------- Class variables ----------------------
'Normally all class variables are private and you use Lets, Gets, and Sets accessors to retrieve and set.
'Common to start with m to mean 'member' of a class.

Private WithEvents m_TextBox As Access.TextBox
Private m_DisplayLabel As Access.Label

'---------------------------- My Initialize -------------------------------------------------
'Most languages have what is called a parameterized contructor for the class. Does not exist in vba
'This is my way of cheating and I do this on most classes, but not required.
'It is just a method to set a lot of properties

Public Sub Initialize(TheTextBox As Access.TextBox, TheDisplayLabel As Access.Label)
  Set Me.TextBox = TheTextBox
  Set Me.DisplayLabel = TheDisplayLabel
  'Ensure events will be raised
  With Me.TextBox
    .OnGotFocus = "[Event Procedure]"
    .OnLostFocus = "[Event Procedure]"
  End With
End Sub

'--------------------------------Property Accessors: Let, Get, Set -------------------------------

Public Property Get TextBox() As Access.TextBox
    Set TextBox = m_TextBox
End Property

Public Property Set TextBox(ByVal objNewValue As Access.TextBox)
    Set m_TextBox = objNewValue
End Property
Public Property Get ToString() As String
  ToString = "TextBox:" & Me.TextBox.Name & ", Display: " & Me.DisplayLabel.Name
End Property
Public Property Get DisplayLabel() As Access.Label
  Set DisplayLabel = m_DisplayLabel
End Property

Public Property Set DisplayLabel(ByVal objNewValue As Access.Label)
    Set m_DisplayLabel = objNewValue
End Property

'****************************************************************************************************************************************************************
'-----------------------------------------------------------------------------------  Trap Events   -------------------------------------------------------------
'*****************************************************************************************************************************************************************

Private Sub m_TextBox_GotFocus()
  Me.DisplayLabel.Caption = Me.TextBox.Name & " Has Focus."
End Sub

Private Sub m_TextBox_LostFocus()
  Me.DisplayLabel.Caption = ""
End Sub

To manage a collection class just modify this code

Code:
Private m_LocateableTextBoxes As Collection

Public Function Add(TheTextBox As Access.TextBox, TheDisplayLabel As Access.Label) As Locateable_TextBox
  'create a new LocateableTextBox and add to collection
  Dim NewLocateableTextBox As New Locateable_TextBox
  NewLocateableTextBox.Initialize TheTextBox, TheDisplayLabel
  m_LocateableTextBoxes.Add NewLocateableTextBox, NewLocateableTextBox.TextBox.Name
  Set Add = NewLocateableTextBox
 End Function

Public Sub Add_LocateableTextBox(ByVal TheLocateableTextBox As Locateable_TextBox)
  'I also add a second Add to allow you to build the object and then assign it
   m_LocateableTextBoxes.Add TheLocateableTextBox, TheLocateableTextBox.TextBox.Name
End Sub

Public Property Get Count() As Long
  Count = m_LocateableTextBoxes.Count
End Property

Public Property Get Item(Name_Or_Index As Variant) As Locateable_TextBox
  Set Item = m_LocateableTextBoxes.Item(Name_Or_Index)
End Property

Sub Remove(Name_Or_Index As Variant)
  'remove this person from collection
  'The name is the key of the collection
  m_LocateableTextBoxes.Remove Name_Or_Index
End Sub

Public Property Get ToString() As String
  Dim strOut As String
  Dim i As Integer
  For i = 1 To Me.Count
    strOut = strOut & Me.Item(i).ToString & vbCrLf
  Next i
  ToString = strOut
End Property


'----------------------------------------------- All Classes Have 2 Events Initialize and Terminate --------
Private Sub Class_Initialize()
 'Happens when the class is instantiated not related to the fake Initialize method
 'Do things here that you want to run on opening
  Set m_LocateableTextBoxes = New Collection
End Sub

Private Sub Class_Terminate()
  'Should set the object class properties to nothing
  Set m_LocateableTextBoxes = Nothing
End Sub

To use the class just use the Collection Class
Code:
Private LTBs As New Locateable_TextBoxes

Private Sub Form_Load()
  Dim ctrl As Access.Control
  For Each ctrl In Me.Controls
    If ctrl.Tag = "L" Then
      LTBs.Add ctrl, Me.lblDisplay
    End If
  Next ctrl
    
End Sub
 

Attachments

I think I have got it now from the DB A.P.R. Pillai created
That code is not very clean IMO. Way to many moving parts with stand alone module and some of the other design. If you are going to bother writing a class why not encapsulate all of the functionality into the class? It is kind of defeating the purpose of OOP.
 
Do you still need the Let & Get if you make the textboxes out of a class?
Let, gets, and Sets are called accessors. They let you "access" the properties without touching the class variables directly. You do not need them you can access public class variables. I almost always add them for added flexibility.
If you had a public variable called display
you can call that variable directly
myObj.Display without a Let/Get named Display.

FYI. I use MZTOOLS which builds all the Let/Get/Sets so there is really no extra time needed to create the Accessors.
 
I like to use an “event bridge” for something like this (if the simple use of a function assignment is not sufficient).

Concept:
Several instances of a class react to the events of their TextBox and forward them to another class (only one instance) via a method call.
The “collecting instance” again triggers an event that is reacted to in the form.

This creates a reusable class structure, as only general event forwarding takes place and the actual code for formatting etc. is designed in the form.
 

Attachments

Last edited:
As @CJ_London points out this does not require a class since a function can trap many events.

I do not try to make the "classes" minimal. I always try to make them easy to use with lots of functionality.
If you want to highlight the selected textbox then here is more relevant. But I usually add a lot of optional parameters so in this case they can tailor the forecolor, backcolor, borderwidth, bordercolor.

But this is the entire code the user would need to be concerned with. And they can modify the properties or use the defaults

Code:
Private LTBs As New HighLightControls

Private Sub Form_Load()
  Dim ctrl As Access.Control
  For Each ctrl In Me.Controls
    If ctrl.Tag = "L" Then
      LTBs.Add ctrl, RGB(255, 0, 100), RGB(100, 225, 0), 4
    End If
  Next ctrl
End Sub
 

Attachments

Sadly, that is a Bridge Too Far :) for me at the moment.
I am just getting my head around assign the control to a class.
 

Users who are viewing this thread

Back
Top Bottom