473,327 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Loop and addressing other controls

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
3 1305
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
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
* "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ryan Ternier | last post by:
Thanks for the previous help guys! I got my list box issue working, but now i'm trying to loop through all the items in my page. I want to find each listbox, once I do i strip the ID down to...
4
by: sck10 | last post by:
I changed my aspx page to use a master page. The problem is that I can no longer loop through the controls on the content page. My question is how do you loop through the controls on the master...
3
by: Kezza | last post by:
Hi There.. I have dynamically created some textbox and checkbox controls by adding them to a panel. Now I would like to get the values out. I have created a for each loop that I can see the...
8
by: dominique | last post by:
Hi, Is it possible (in vb.net with WinForms) to loop throw controls inside a container (form or panel) sorting the controls on a property (.tabindex for example) ? My problem : on several...
3
by: JebBushell | last post by:
I am unable to configure the addressing for images so that I can see them in a) the VS 2003 IDE, b) the site as hosted on my local machine, and c) the site as hosted in production. As an...
5
by: Ed Jay | last post by:
I have a switch statement that controls which of several containers is displayed or not. It currently looks like: function showHelp(n) { show('vhelp'); //makes parent container visible switch...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
0
by: gerritmitchell | last post by:
Hi, I have a situation where I need to send a SOAP message from a receiver through multiple intermediaries and then to an ultimate receiver. The intial sender will tell the intermediary where...
1
by: =?Utf-8?B?dWx0cmFuZXQ=?= | last post by:
We have a client that uses .Net that needs to work against our Java (xfire) based WS. My question is: how can a .Net (C#) WS client be configured to not send WS-Addressing headers? The client in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.