Re: Loop and addressing other controls

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Imran Koradia (nojunk_at_microsoft.com)
Date: 08/29/04


Date: Sat, 28 Aug 2004 22:55:46 -0400

There are 2 ways you can do this:

One is to loop through the controls in frmLC and find the one you are
looking for:

If lblP1JoyUp.BorderStyle = BorderStyle.FixedSingle then
    .
    .
    'all your label stuff
    .
    .
    'call one of the two following methods:
    CheckMyCheckBox(lblP1JoyUp.Name)

    'CheckMyCheckBoxReflection(lblP1JoyUp.Name)
End If

Private Sub CheckMyCheckBox(sLabelName as String)
    dim sCheckBoxName as String
    sCheckBoxName = "chk" & sLabelName.Substring(3)
    For Each ctl as Control in frmLC.Controls
        If TypeOf ctl Is CheckBox Then
            If ctlName = sCheckBoxName Then
                DirectCast(ctl, CheckBox).CheckState = CheckState.UnChecked
            End If
        End If
    Next ctl
End Sub

Note above that this wont find controls that are within another control such
as a panel or tab. Nevertheless, you can just extend that to loop
recursively. But since you were accessing the checkboxes from the form
itself (frmLC.chkP1JoyUp), I assumed thats where your checkboxes are and so
I didn't bother recursing.

The other way is using Reflection:

Private Sub CheckMyCheckBoxReflection(sLabelName As String)
    Dim oPropInfo As System.Reflection.PropertyInfo
    Dim oObj As Object

    sCheckBoxName = "chk" & sLabelName.Substring(3)

    oPropInfo = frmLC.GetType.GetProperty(sCheckBoxName,
Reflection.BindingFlags.Instance _
                                                        Or
Reflection.BindingFlags.NonPublic _
                                                        Or
Reflection.BindingFlags.Public)
    If Not oPropInfo Is Nothing Then
    oObj = oPropInfo.GetValue(ofrm, Nothing)
        If TypeOf oObj Is Control Then
            If TypeOf oObj Is CheckBox Then
                DirectCast(oObj, CheckBox).CheckState = CheckState.UnChecked
            End If
        End If
    End If
End Sub

I guess if you have a lot of controls on frmLC then looping through would
surely be slow since you would now have 2 loops - one looping your labels on
one form, and then for each label you are looping the controls on another
form. Ofcourse, I dont know how efficient the reflection method is either.
Anyway, use what fits you best.

hope this helps..
Imran.

"jcrouse" <me> wrote in message
news:OW4gx7VjEHA.1356@TK2MSFTNGP09.phx.gbl...
>I am trying trying to loop through some label controls and setting some
> properties for the labels I'm looping through. Currently I am addressing
> the
> labels one at a time with IF...Then logic, like this:
>
>
>
> If lblP1JoyUp.BorderStyle = BorderStyle.FixedSingle Then
>
> lblP1JoyUp.Top = 10
>
> lblP1JoyUp.Left = 10
>
> lblP1JoyUp.Height = 24
>
> lblP1JoyUp.Width = 100
>
> lblP1JoyUp.Visible = False
>
> lblP1JoyUp.BackColor = Color.Transparent
>
> lblP1JoyUp.ForeColor = Color.White
>
> lblP1JoyUp.Font = frm1.Font
>
> lblP1JoyUp.BorderStyle = BorderStyle.None
>
> frmLC.chkP1JoyUp.Checked = False
>
> End If
>
>
>
> It checks to see if a label is selected (Borderstyle.FixedSingle) and if
> so.
> Changes a bunch of the labels properties. I don't have a problem changing
> this code with my loop. However, the last line sets the status of a
> checkbox
> on a different form. As you can see the checkbox name corresponds with the
> labelname. I can't seem to figure out how to address the checkbox in my
> loop
> code. Here is my loop code:
>
>
>
> For Each ctrl As Label In Me.Controls
>
> If TypeOf ctrl Is Label Then
>
> If ctrl.BorderStyle = BorderStyle.FixedSingle Then
>
> 'Dim strlblCheck As CheckBox
>
> 'strlblCheck.Name = "chk" & ctrl.ToString
>
> ctrl.Top = 10
>
> ctrl.Left = 10
>
> ctrl.Height = 24
>
> ctrl.Width = 100
>
> ctrl.Visible = False
>
> ctrl.BackColor = Color.Transparent
>
> ctrl.ForeColor = Color.White
>
> ctrl.Font = frm1.Font
>
> ctrl.BorderStyle = BorderStyle.None
>
> 'frmLC.strlblcheck.Checked = False
>
> End If
>
> End If
>
> Next
>
>
>
> You can see by the commented lines that I've tried a few things, but no
> luck yet. What do I need to do here?
>
>
>
> Thanks,
>
> John
>
>



Relevant Pages

  • Re: NEWBIE Show different name in datasheet?
    ... If you have a subform shown in Datasheet View, ... The same applies for checkbox controls, combo boxes and any other datatype ... > associated label, and that label has a caption property that typically is ...
    (comp.databases.ms-access)
  • Re: Using CheckboxX in a loop
    ... loop x (total number of controls) times. ... The neater way is to stick together the string "Checkbox" and the ... Private Sub UserForm_Initialize ...
    (microsoft.public.word.vba.userforms)
  • Re: Controls left behind on a resize
    ... Paint event of the form. ... Look at what the loop will do: ... // Second label is now at index 0, third label is at index 1. ... In cases like this, you need to loop _backward_ through the controls, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Im Pulling My Hair out!!
    ... sounds like your labels are not associated with the checkbox controls. ... when you build a report, either with a wizard or by creating the ... controls manually, each control automatically has a label "attached" to it. ...
    (microsoft.public.access.gettingstarted)
  • Re: Variable Type?
    ... Label pobjLabel = pobjControl as Label; ... > I have a page with numerous label controls with the Visible attribute set to> false. ... > On page_load I loop through an array and depending on what is returned for> each label, I want to set the Visible attribute to true as well as a number> of other attributes. ...
    (microsoft.public.dotnet.languages.csharp)