By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Loop and addressing other controls

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
John,

My favorite recursive control loop documentated in this message (typed so
watch typos)

I thought everything was in this answer you was asking.

I hope this gives an idea?

Cor
\\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
'It starts on a top place, can be any place by instance
'a button event, however it starts with Me which is
'the form itself which inherits from "Control"
'because it takes only the objects which inherits from "Control"
'it uses early binding
End Sub
'------------------
Private Sub doSet(ByVal thisCtr As Control)
Dim ctr As Control
'a placeholder for the reference of the object is created
For Each ctr In thisCtr.Controls
'The first time each (parent)control on a form
If TypeOf ctr Is Label
'Look if the "Control" is a label
ctr.text = "This you can do for every property from that label
End if
doSet(ctr)
'check if the control has children and do for that the same
Next
End Sub
///
Nov 21 '05 #3

P: n/a
* "jcrouse" <me> scripsit:
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.


\\\
Private Function FindControl( _
ByVal ControlName As String, _
ByVal CurrentControl As Control _
) As Control
Dim ctr As Control
For Each ctr In CurrentControl.Controls
If ctr.Name = ControlName Then
Return ctr
Else
ctr = FindControl(ControlName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function
///

Usage:

\\\
DirectCast(FindControl("Button1", Me), Button).Enabled = False
///

Notice that the procedure listed above is "slow", if you have to access a
lot of controls by name very often, you should store references to them in a
'Hashtable' object. You can use the name of the control as key:

\\\
Private m_Controls As New Hashtable()
///

Adding a control:

\\\
Dim DynamicPictureBox As New PictureBox()
DynamicPictureBox.Name = "PictureBox1"
m_Controls.Add(DynamicPictureBox.Name, DynamicPictureBox)
///

Looking for a control:

\\\
Dim p As PictureBox = DirectCast(m_Controls.Item("PictureBox1"), PictureBox)
///

Removing a control:

\\\
m_Controls.Remove("PictureBox1")
///

Sometimes it's even better to add the control to an array. This will allow
fast and easy index-based access to the control references:

\\\
Dim MyLabels() As Label = {Label1, Label2, ..., Label10}
///

Access by 'MyLabels(0)' to 'MyLabels(9)'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.