Solved

How to reference control array objects generically

Posted on 2006-06-24
4
270 Views
Last Modified: 2010-04-07
In my VB6.0 App I have a Public Sub 'Update_Form_Controls' that I call from each of the dozens of forms in the app.  The purpose of the sub is to make sure that the fonts, foreground colors and various other attributtes are consistent for most of the controls on all of the forms in the app.  We had several developers work on the forms and they didn't always adhere to the standards.  This is a way to clean up without revising each non-standard control individually.  I realize that I take a performance to invoke this on each form but it needs to be done.

Here is some of the Sub's code that I am using for labels:

Public Sub Update_Form_Controls(ByRef frm As Form)
 
  Dim c As Control
  For Each c In frm.Controls
    '
    controlType = Trim(UCase(TypeName(c)))
    controlName = Trim(UCase(c.Name))
    '
    Select Case UCase(TypeName(c))
    Case Is = "LABEL"
        If UCase(Mid(c.Name, 1, 7)) = "LBLVLOW" Or _
           UCase(Mid(c.Name, 1, 6)) = "LBLLOW" Or _
           UCase(Mid(c.Name, 1, 8)) = "LBLVHIGH" Or _
           UCase(Mid(c.Name, 1, 7)) = "LBLHIGH" Or _
           UCase(Mid(c.Name, 1, 6)) = "LBLCDP" Or _
           UCase(Mid(c.Name, 1, 13)) = "LBLDRAFTCOLOR" Or _
           UCase(Mid(c.Name, 1, 11)) = "LBLRFRCOLOR" Or _
           UCase(Mid(c.Name, 1, 13)) = "LBLERRORCOLOR" Or _
           UCase(Mid(c.Name, 1, 11)) = "LBLCDPCOLOR" Or _
           UCase(Mid(c.Name, 1, 11)) = "LBLTRANSFERCAPTION" Or _
           UCase(Mid(c.Name, 1, 16)) = "LBLCOMPLETECOLOR" Then
        Else
          c.BackStyle = 0
    '  certain labels should not be resized or font altered.


As you can see with lables, some of the the controls are execptions and I either don't want to alter the attributes or I want them to have their own unique attributes.  In this example I am identifying the by their names.  To reduce the number of contols on one of the forms I used a control array 'lblfrmDesc' for all of the 35 labels on the form.  Within those 35 labels their are some that I want to handle uniquely.  For example: lblfrmDesc(10) and lblfrmDesc(11) have attributes defined on them that I don't want to change.  I want all of the other labels in the control array to have a tranparent background, but these should be opaque.  I can't figure out how to reference a unique occurrence of a control array in the Public Sub that is used by all of the forms in the system.  If the Sub were private and on the form in question, I would could refer to the controls as lblfrmDesc(10).backstyle but since it is public and I pass the form into it I don't have that option.

How do I reference lblfrmDesc(10).backstyle in my public subroutine?  Gnerally how do I reference a specific element of a control array in my subroutine.
0
Comment
Question by:mlcktmguy
4 Comments
 
LVL 10

Assisted Solution

by:cool12399
cool12399 earned 50 total points
ID: 16975618
confused exactly what the problem/situation is, but you would reference it with the form variable.
Since you have 'frm' being passed in by reference, you would use:

frm.lblfrmDesc(10).BackStyle

and if you are trying to use a generic function (i.e., some of the time you'll have a label that exists
under that name, and sometimes you won't), then simply enclose it in between 'on error' statements, i.e.,

on error resume next
frm.lblfrmDesc(10).BackStyle=12345
on error goto 0

that way, if that particular label name doesn't exist for that particular form, it will simply skip the statement.
0
 
LVL 1

Author Comment

by:mlcktmguy
ID: 16975750
Thanks for the response.  It is almost what I was looking but I re-read my question and see that didn't explain it well.

frm.lblfrmDesc(10) on form 'frmDescription' is one of the fields that had its attributes set correctly at design time.  One of those attributes is backstyle but there are many others that make it non-standard.  Rather than set the attributes in my public subroutine I want to be able to identify frm.lblfrmDesc(10) on form 'frmDescription' and bypass all my standardized atribute logic.  In pseudo code it woulld look like

If this control is on 'frmDescription' and it is occurrence 10 or 11 of label control 'lblfrmDesc' then
    don't do anything
else
    set all of the 'standard' attributes
endif



In my generic subrouitne I want to be able to identify frm.lblfrmDesc(10).BackStyle=12345
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 75 total points
ID: 16976542
You can use code like this to determine if you have the target controls:

Public Sub Update_Form_Controls(ByRef frm As Form)
    Dim c As Control
    Dim lbl As Label
    For Each c In frm.Controls
       
        If TypeOf frm Is frmDescription Then
            If TypeOf c Is Label Then
                Set lbl = c
                If lbl.Name = "lblfrmDesc" Then
                    If lbl.Index = 10 Or lbl.Index = 11 Then
                        lbl.Caption = "You found me..."
                    End If
                End If
            End If
        End If
       
    Next c
End Sub
0
 
LVL 9

Expert Comment

by:lojk
ID: 16982280
In my experience, i would strongly recommend changing the source forms directly rather than impacting on application performance at runtime especially if as i supsect you have hundreds of controls on any one form... Open a couple up in notepad and see how there are start and end blocks that you can change the properties within.

write a routine to open each form, do a readline into a collection/listbox or similar, then for each line check that the required property is at the required value, save te collection back out to the file.. BTW in this case each control begin and end block will list the control index and its name.

Job done, once only... (you could knock up a quick Add-in to enable this functionality from the design time environment if you need to do this on a regular basis; worth a look)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question