473,837 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Bord erStyle = BorderStyle.Fix edSingle Then

lblP1JoyUp.Top = 10

lblP1JoyUp.Left = 10

lblP1JoyUp.Heig ht = 24

lblP1JoyUp.Widt h = 100

lblP1JoyUp.Visi ble = False

lblP1JoyUp.Back Color = Color.Transpare nt

lblP1JoyUp.Fore Color = Color.White

lblP1JoyUp.Font = frm1.Font

lblP1JoyUp.Bord erStyle = BorderStyle.Non e

frmLC.chkP1JoyU p.Checked = False

End If

It checks to see if a label is selected (Borderstyle.Fi xedSingle) 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.BorderStyl e = BorderStyle.Fix edSingle Then

'Dim strlblCheck As CheckBox

'strlblCheck.Na me = "chk" & ctrl.ToString

ctrl.Top = 10

ctrl.Left = 10

ctrl.Height = 24

ctrl.Width = 100

ctrl.Visible = False

ctrl.BackColor = Color.Transpare nt

ctrl.ForeColor = Color.White

ctrl.Font = frm1.Font

ctrl.BorderStyl e = BorderStyle.Non e

'frmLC.strlblch eck.Checked = False

End If

End If


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?


Nov 21 '05 #1
3 1320
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.Bord erStyle = BorderStyle.Fix edSingle then
Nov 21 '05 #2

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?

Private Sub Form1_Load(ByVa l sender As Object, _
ByVal e As System.EventArg s) Handles MyBase.Load
'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.Control s
'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
'check if the control has children and do for that the same
End Sub
Nov 21 '05 #3
* "jcrouse" <me> scripsit:
It checks to see if a label is selected (Borderstyle.Fi xedSingle) 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

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
ctr = FindControl(Con trolName, ctr)
If Not ctr Is Nothing Then
Return ctr
End If
End If
Next ctr
End Function


DirectCast(Find Control("Button 1", 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 DynamicPictureB ox As New PictureBox()
DynamicPictureB ox.Name = "PictureBox 1"
m_Controls.Add( DynamicPictureB ox.Name, DynamicPictureB ox)

Looking for a control:

Dim p As PictureBox = DirectCast(m_Co ntrols.Item("Pi ctureBox1"), PictureBox)

Removing a control:

m_Controls.Remo ve("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

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 find what I need so I can populate a DB. lstTest is dim'd as a ListBox, and I thought I could just do:
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 page and how do you loop through the controls on the content page when the content is inside the ContentHolder control? Below is an example of the control loop before using a master page. Sub FindControlsInWebForm() Dim strValue As String = ""
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 controls Unique ID with. But I for the life of me can't figure out how to see the value the control is storing. Could somebody please help me. I'm thinking that I might need to use something like "findcontrol", but this wouldn't make sense to me. ...
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 forms (or panels), i have many controls (inherited controls from base controls : MyTextbox, MyCombobox ..). I add a new property on these controls : .BeginningGroup (boolean : yes or no).
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 example, I have an <asp:image...> on the same page as an <IMG...>. Both controls are specified to display the same image file. However, in order to see both render image at the same in development, I need to address one as ../ and the other as ./ I...
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 (n){ case 0: show('image0');hide('image1');hide('image2');...;hide('imagem'); break; ..
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 instructions which can sure be optimized away is the check for the break condition, at least within the time where it is known that the loop will not reach it. Any idea how to write such a loop? e.g.
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 to send the message to next within the service chain. SOAP targeted headers and SOAP roles seem to suppor this quite well. My question is can this be used in conjunction with WS-Addressing? Meaning can I send a WS-Addressing compliant message...
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 particular mentions having tried WSE 2.0, and WSE 3.0, and says there is no way to turn off WS-Addressing in a WS client, using those frameworks. I find that very hard to believe. There appears to be a problem w/ WS-Addressing headers in the...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.