Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find all Label controls in a Frame 1

Status
Not open for further replies.

waytech2003

Programmer
Jul 14, 2003
316
US
I have 14 Frames on a Form that each contain about 20 Labels. All labels are of a single array Label1(xxx). This can not change because the Lables captions are filled from a database when Form is loaded.

I would like to change the background color of the Labels when I mouseover given Frame.

How can I find only the Labels in the desired Frame?

I currently am using the code below, but that looks at all the controls on the Form and I would like it to be quicker.

Code:
Dim xCount       As Integer
Dim m_Selected   As Integer
Dim CTL          As Control
Dim WorkingFrame As Frame

Set WorkingFrame = Frame1(m_Selected)
For Each CTL In Me.Controls
   If TypeOf CTL Is Label Then
      If CTL.Container = WorkingFrame Then CTL.BackColor = vbGreen
    End If
Next
 
You could use the MouseMove event of one of the labels in the frame to change the colour of all the labels in that array.



[gray]Experience is something you don't get until just after you need it.[/gray]
 
Perhaps;

Sub UpdateLabels(FrameID As Frame)

Dim lab As Label

For Each lab In Label1
If lab.Container = FrameID.Caption Then
lab.BackColor = vbGreen
Else
lab.BackColor = vbWhite
End If
Next

End Sub
 
Thanks for the replies

Error7, I believe you did not understand the Label1(array). All Labels on the Form are of one array. Label1(1 to 280+/-) so I can not look at a different array for each Frame. Also each Frame has a varying number to Labels so I can not just say 1 to 20 is Frame1(1), and 21 to 40 is Frame1(2)

HughLerwill, Yes that would be slightly faster as it only looks at Labels controls on the Form.

I guess no one has a solution to just look at the controls contained in a given Frame, and not look at all the controls on the Form. I was hoping for a similar property as Me.Controls for a Form.
 
How about this. I use this to find whats populated in a frame.

Code:
Function WhatsPopulated(frm As Form, Frame As Frame, Optional ByRef Total As Integer) As String

Dim ctl     As Control

With ctl
     For Each ctl In Frame.Container
      If Not ctl.Name = Frame Then
    'TYPENAME RETURNS THE TYPE OF CTL.  TEXTBOX, COMBOBOX, LABEL ETC.
        Select Case TypeName(ctl)
            Case "TextBox", "ComboBox"
                If ctl <> "" Then
                    If WhatsPopulated = "" Then
                        WhatsPopulated = ctl.Name
'                        Total = ctl.Tag
                    Else
                        WhatsPopulated = WhatsPopulated & "," & ctl.Name
'                        Total = Total + ctl.Tag
                    End If
                End If
        End Select
       End If
    Next
End With

End Function
Called like this
Code:
IF WhatsNotPopulated(frmMain,Frame1) > vbnullstring then
  MsgBox "All fields required!"
  exit sub
end if
 
CTL in Frame.Container"

That's exactly what I needed...

Thanks all for your replys
 
oops

I miss spoke Frame.Container in this case in the Form, and all Controls on the Form are still looked at.

Code:
dim CTL as contol
For Each CTL In Myframe.Container
   If TypeOf CTL Is Label Then
      CTL.BackColor = vbGreen
   End If
Next

I want to look only at one perticular Frame's Labels, and not bother wasting time looking at all other controls on the Form
 
Waytech2003,

I added a text box outside my frame and added ctl.backcolor = vbgreen to the function. Only the text boxes inside
my frame turn green. The ones I added on the outside stayed white?
 
OOPS. Now I'm wrong. I am still going through all the controls. A slightly different version of what I posted earlier, I thought was the answer, but it's not.

Code:
For each ctl in frm.Controls

  If ctl.container Is Frame then
     .
     .

But that still loops through all the controls.
 
If the number of Labels in each Frame remains constant, you could hard code it using stuff like;

Select Case CurrentFrame.Index
Case 0
For i = 0 to 10
Label1(i).Backcolor = vbGreen
Next
Case 1
For i = 11 to 25
'etc.
End Select

I guess this is what you would like to avoid though re. self maintenance of the code eg. if you insert another Label. Should be quick though!
 
Iv'e been looking at this for the past couple of days and through expermentation I have found that you could create a 2d array...

SomeArray(frame index, count of labels contained by frame) = index of label to color

However, if you used this method and hovered over frame index 0 and then hovered over frame index 1 then you would have to somehow keep track of which frame was colored and needed to be returned to non hovering color.

So by the time I had this all figured out the amount of code required to load the array and keep track of everything is just a pain in the ... and I say this because it is quite easier to use a variation of hugh's code...

[tt]
Option Explicit

Dim LastIndex As Integer

Private Sub Form_Load()
LastIndex = -1
End Sub

Private Sub Frame1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

On Error GoTo Frame1_MouseMoveError

Dim C As Control

If LastIndex = Index Then Exit Sub

For Each C In Me.Controls

If TypeOf C Is Label Then

If C.Container.Name = "Frame1" Then

If C.Container.Index = Index Then

C.BackColor = vbGreen

Else

C.BackColor = vbButtonFace

End If

End If

End If

Next

LastIndex = Index

Exit Sub
Frame1_MouseMoveError:

MsgBox "Frame1_MouseMove: " & Err.Number & ":" & Err.Description

End Sub
[/tt]


Good Luck

 
HughLerwill, the number of Labels in each Frame does not remain constant, so hard coding will not work.

vb5prgrmr, All samples, including my original code, that use
Code:
For Each CTL In Me.Controls
loop thru all controls on the Form. I am able to determine which Labels are in the current Frame with my existing code
Code:
If CTL.Container = WorkingFrame
and change them only.

My goal was to speed things up by only looping thru the controls (Labels) contained in the selected Frame. If the Frame control does not have a property like Me.Controls, I guess I can not do this.

I wonder if there is an API that would expose this property?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top