Function identifying calling object

corpuschristi

New member
Local time
Today, 11:15
Joined
Jun 23, 2008
Messages
4
First timer here, so, naturally, feeling you may find my question trivial, as my familiarity with vb programing is poor, but please bear with me. And please excuse my English.

I'm building a form, containing hundreds of labels. Now, i want to create a function that changes the background color of a label to red, when clicked upon, and back to white when clicked again, and so on.
I created a module, so far it looks like this:

Function PaintCell(ObjName As Object)

If (ObjName.BackColor = RGB(255, 255, 255)) Then
ObjName.BackColor = RGB(255, 0, 0)
Else
ObjName.BackColor = RGB(255, 255, 255)
End If

End Function

It works fine, BUT
I want each of the 24x24 (576) labels to call the same function when clicked (event-when clicked: =PaintCell(forms!formname!objectname)). I'm trying to save some work. Sure, i could go through each of the labels and change the event, the parameter of the function to the full object path, but it would take hours. Is there a way for a function to identify the object (for this matter, the label) who'd called it, and then execute commands on it, without its name recieved as parameter? This way, i could just create a single label, set everything, then COPY-PASTE it to create as many as i need.
If it can be done, or if there are other possibilities, i would be grateful for any assistence.

Andrej.
 
Hi.

Often (with controls other than labels) you can fall back on the ActiveControl property to determine which control had the focus when the function was called.
But that's not an option here.

Some options (from the rediculous to the reasonable)...
• You could attempt to determine the cursor location on screen relative to the controls and hence which control was under the cursor when clicked (just to make clear - that, IMO, falls under the "rediculous" category)...
• You can run code as your form opens which assigned the event property to each control - using its name as part of the expression
• A variation of the above - but done at design time (in design view) to permanently assign the event property, but just saves you the typing.
• You could use a class to sink the events of the labels (by adding them all to a collection or array) which responds by the clicking of any given one.

The easiest one to demonstrate is the second - something like

Code:
For Each ctl in Me.Controls
    If ctl.ControlType = acLabel AND ctl.Tag = "UseMe" Then
        ctl.OnClick = "=PaintCell(" & ctl.Name & ")"
    End If
Next

That sort of thing.
 
Just to make clear - only the last of the methods I listed wouldn't pass the name of the control - however the class(es) would still pass a control object to the function (which is what you were doing anyway ;-)
 
Thanks, LPurvis, for replying. I tried using the code segment you added within the form's On Open event, but it doesn't work. No error, nothing happens. There must be something i'm doing wrong. What does "ctl" stand for? Is it a variable i need to declare? And ctl.tag, is it the text string of the label?
I'm sorry, but as i said, my experience with vb is lacking, to say the least.
If possible, could you please add the whole procedure code?
 
I'd imagine it didn't do the business for you (not that it was intended to right out of the box - it is air code) is due to my addition of the ctl.Tag = "UseMe" check.
You'll not have that implemented...

Basically - I was guessing that you'll have label controls other than those you're wanting to employ this (presumably) grid type formatting to? (Labels as children of textboxes etc?)
So I was indirectly suggesting that placing a Tag property of UseMe in the labels you want to perform this upon to identifying them.

Of course - that might be an effort. So you could go the other way with it and exclude the other labels you don't want this on by providing a Tab property (say "IgnoreMe" ;-)
Then that line of code could become

If ctl.ControlType = acLabel AND ctl.Tag <> "IgnoreMe" Then

Does that make sense?
Are you familiar with the tag property?
Look on the property sheet for it if not.
 

Users who are viewing this thread

Back
Top Bottom