Author Topic: Event procedures for Controls created at Run-time  (Read 8173 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Event procedures for Controls created at Run-time
« Reply #15 on: August 16, 2005, 01:49:48 PM »
Am I to presume you are creating this in AutoCAD or is it in some other environment
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kheilmann

  • Guest
Event procedures for Controls created at Run-time
« Reply #16 on: August 16, 2005, 02:04:27 PM »
It is in Excel, although I am not using any Excel functions.
Everything is 100% VBA (except for my *.mdb files)  But since you don't need Access to access them, I don't count Access as being part of this either.

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #17 on: August 16, 2005, 02:12:45 PM »
I'd like to see a group example myself, Keith. I didn't know you could use that for a control array.

In the meantime here is how I would do it without getting too complex:
1. Create a class that uses the checkbox variable using withevents.
2. push the click event inside the class
3. Create a collection class to manipulate (add, delete) your controls as they are added, etc.
4. In your form code, add as you need.
5. If you need specific control (say to sync the database with the particular row your clicking) add a property to the class to keep track of which one is being clicked. I like the tab property myself.

I threw together a quick demo;

The cCheck class:
Code: [Select]

' Class cCheck
' Class to encapsulate the check box events
Option Explicit

Private WithEvents mvarmCheck As CheckBox 'local copy
Public Property Set mCheck(ByVal vData As CheckBox)
'used when assigning an Object to the property, on the left side of a Set statement.
'Syntax: Set x.mCheck = Form1
    Set mvarmCheck = vData
End Property

Public Property Get mCheck() As CheckBox
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.mCheck
    Set mCheck = mvarmCheck
End Property

' Here is where you do your stuff
Private Sub mvarmCheck_Click()
    MsgBox "The Click Event Inside the Class Module"
End Sub


The collection class (just a class):
cChecks
Code: [Select]

' Collection Class: cChecks
' used to hold the cCheck objects
Option Explicit

'local variable to hold collection
Private mCol As Collection

Public Function Add(mCheck As CheckBox, Optional sKey As String) As cCheck
    'create a new object
    Dim objNewMember As cCheck
    Set objNewMember = New cCheck
    'set the properties passed into the method
    If IsObject(mCheck) Then
        Set objNewMember.mCheck = mCheck
    Else
        objNewMember.mCheck = mCheck
    End If
    If Len(sKey) = 0 Then
        mCol.Add objNewMember
    Else
        mCol.Add objNewMember, sKey
    End If
    'return the object created
    Set Add = objNewMember
    Set objNewMember = Nothing
End Function

Public Property Get Item(vntIndexKey As Variant) As cCheck
    'used when referencing an element in the collection
    'vntIndexKey contains either the Index or Key to the collection,
    'this is why it is declared as a Variant
    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
  Set Item = mCol(vntIndexKey)
End Property

Public Property Get Count() As Long
    'used when retrieving the number of elements in the
    'collection. Syntax: Debug.Print x.Count
    Count = mCol.Count
End Property

Public Sub Remove(vntIndexKey As Variant)
    'used when removing an element from the collection
    'vntIndexKey contains either the Index or Key, which is why
    'it is declared as a Variant
    'Syntax: x.Remove(xyz)
    mCol.Remove vntIndexKey
End Sub


Public Property Get NewEnum() As IUnknown
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property


Private Sub Class_Initialize()
    'creates the collection when this class is created
    Set mCol = New Collection
End Sub


Private Sub Class_Terminate()
    'destroys collection when this class is terminated
    Set mCol = Nothing
End Sub


The user form code:
Code: [Select]

' UserForm1
Option Explicit
' NOTE: Scope is important
Private ochecks As cChecks
Private oCheck As cCheck

' add the checkboxes and push to the collection class
Private Sub UserForm_Activate()
    Set ochecks = New cChecks
    Dim oChk As CheckBox
    Set oChk = Me.Controls.Add("Forms.Checkbox.1", "MyTestCheck", True)
    Call ochecks.Add(oChk, "1")
    Set oChk = Me.Controls.Add("Forms.Checkbox.1", "MyTestCheck1", True)
    oChk.top = 50
    Call ochecks.Add(oChk, "2")
End Sub


Lotta code, but not rocket science and if you have visual basic, it will code most of the collection class for you.
Hope that helps..

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Event procedures for Controls created at Run-time
« Reply #18 on: August 16, 2005, 02:20:48 PM »
In VBA, you can set the GroupName property of a control to make it part of a group.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #19 on: August 16, 2005, 02:28:11 PM »
hmm. I see.

I've used that to seperate different groups of option buttons in the same container.

But I've never seen it used to create a control array.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Event procedures for Controls created at Run-time
« Reply #20 on: August 16, 2005, 02:43:14 PM »
Well, it doesn't really create an array per'se but it allows you to segregate the controls so you can more easily identify them as being part of a specific group, particularly if you have multiple controls where the name is unknown at runtime ..

When the control is created, you can assign it to a group via groupname (although it does not act like a group as does an option button) it will allow you to verify the group (or array, if you will) that it belongs to ...

For example in a bonefide array, you have controls named such as CheckBox1(0) CheckBox1(1) ... etc ...

In a group you have different names but the group will identify which collection of controls a specific controls belongs to.
I.e. all CheckBox controls could be in Group1 Group2 etc .. thus you could enumerate all controls and act upon the remaining in that group based upon the group name.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Draftek

  • Guest
Event procedures for Controls created at Run-time
« Reply #21 on: August 16, 2005, 03:04:40 PM »
Keith, if you have an example of how that would work to create a single function for an event - like the purpose for a control array, I'd like to see it. That mignt come in handy.

kheilmann: It just dawned on me, you don't have to create a collection class. To make it easier just add a property (Index) to the cCheck class and manipulate it with either an array of cCheck or a simple collection. That would cut down quite a bit of code. You won't need cChecks at all.

Remember to use Redim Preserve when adding to the array.

If you need some help with that just post.