Re: Loop and addressing other controls
From: Imran Koradia (nojunk_at_microsoft.com)
Date: 08/29/04
- Next message: Jonathan Tong: "RAPI with VB.NET Question"
- Previous message: Hal Rosser: "Re: Selecting Proper Variables"
- In reply to: jcrouse: "Loop and addressing other controls"
- Next in thread: Cor Ligthert: "Re: Loop and addressing other controls"
- Messages sorted by: [ date ] [ thread ]
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
>
>
- Next message: Jonathan Tong: "RAPI with VB.NET Question"
- Previous message: Hal Rosser: "Re: Selecting Proper Variables"
- In reply to: jcrouse: "Loop and addressing other controls"
- Next in thread: Cor Ligthert: "Re: Loop and addressing other controls"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|