X10 Community Forum
🖥️ActiveHome Pro => SDK => Topic started by: jtykal on March 03, 2009, 02:33:17 PM
-
I'm just starting to dabble in VB (using Express v8), and would like to have a list of Labels showing the open/close status of each door in my house (DoorState1, DoorState2, DoorState3, etc.). Is there a way to reference a Form Control object (in this case, a Label) using a string variable instead of explicitly naming it? I'm not clear on the terminology I want to use here -- reference, dereference, pointer, etc. come to mind from my programming days -- but that was 20+ years ago! ;)
What I'm looking to do is replace code that looks like this:
Select Case DoorNum
Case 0
DoorState0.Text = "OPEN"
DoorState0.ForeColor = Drawing.Color.Green
Case 1
DoorState1.Text = "OPEN"
DoorState1.ForeColor = Drawing.Color.Green
Case 2
DoorState2.Text = "OPEN"
DoorState2.ForeColor = Drawing.Color.Green
Case 3
DoorState3.Text = "OPEN"
DoorState3.ForeColor = Drawing.Color.Green
...with something cleaner that can append the DoorNum to the end of a fixed string and use the resulting string variable as the name of the Label I want to update. How do I get a pointer to an object in the current Form f I know the name (and class) of the object??
Dim testField As String
Dim testObj As System.Windows.Forms.Label
testField = "DoorState" & DoorNum
testObj = ' get the Label named testField in the current Form
testObj.Text = "OPEN"
testObj.ForeColor = Drawing.Color.Green
I hope that makes sense... Any thoughts? ???
Thanks in advance...
jim
-
Open up the VB help, and search the index for "control arrays".
I think this will tell you what you need to know.
>!
-
It's early and my brain is slow but I have a code snipet from a recent project that may help.
It searches through the list of controls looking for a picturebox using text and a variable to create a control name
c.Name is the control name in my form1.designer.vb file
PictureBoxClicked is a variable with a number, say it is a 10 in this case
search for a control called "PictureBox & "10" - or PictureBox10
' find the picturebox that was clicked and change the volume level graphic
For Each c As Control In Me.Controls
If c.Name = "PictureBox" & PictureBoxClicked Then
Dim PictureBoxName As System.Windows.Forms.PictureBox
PictureBoxName = CType(c, PictureBox)
'This code sets a small .png image for the picturebox that was clicked
PngImage = VolumeArray(Volume) ' get a .png image
PictureBoxName.Image = Image.FromFile(FolderPath & "Images\" & PngImage)
End If
Next
The best place to get VB express help is here.
http://social.msdn.microsoft.com/forums/en-US/Vsexpressvb/threads/
-
Ok, I am awake now, I think this is what you want.
I created a simple form that has a button and 3 labels
Pressing the button each time turns on the forecolor green of each label
It also returns the old label to the color black.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LabelName2 = Label1 ' assign a value to LabelName2
End Sub
Dim LabelName1 As System.Windows.Forms.Label
Dim LabelName2 As System.Windows.Forms.Label
Dim Count As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Count = Count + 1
If Count = 4 Then Count = 1
If LabelName2.ForeColor = Color.Green Then
LabelName2.ForeColor = Color.Black ' return old label to black
End If
For Each c As Control In Me.Controls
If c.Name = "Label" & Count Then
LabelName1 = CType(c, Label)
LabelName1.ForeColor = Color.Green ' new label is now green
LabelName2 = LabelName1 ' old label = new label
End If
Next
TextBox1.Text = "Label # " & Count ' display label number
End Sub
End Class
(http://www.el34world.com/Misc/VB/images/label9.png)
-
EL34 and -Bill-: Thank you both! :)%
I did eventually discover the looping method to search for a control name. I didn't like looping through the 50+ controls on my form to find the one I needed to update; but I learned that if I used a group box to house the similar controls, I could just loop through the 5 or 6 controls inside that group box! This works perfectly, but I'm still hoping to avoid the search loop entirely. I need to investigate -Bill-'s suggestion regarding control arrays. A quick search revealed that control arrays were a VB6 feature and are no longer supported in VB2008, but there was a reference to some other mechanism in VB2008 that rendered control arrays obsolete. Need to dig deeper there...
Thanks again! >!
-
...with something cleaner that can append the DoorNum to the end of a fixed string and use the resulting string variable as the name of the Label I want to update
I looked back at your original post above
If you don't need to loop through controls, and just want to address the exact label you need to change the forecolor on
x is a variable name for the label suffix
In this example, x=2 and so the forecolor will be changed to green on Label2: "Label" & x = Label2
like this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As Integer = 2 ' this would be the label number you want to update
Dim LabelName As Label = Me.Controls("Label" & x)
LabelName.ForeColor = Color.Green
End Sub
-
That's EXACTLY what I was looking for, EL34! Thank you! >!
-
That's what I thought, that's why I re-read your post
I was confused by the control array thing because I thought I remembered you wanting to do something much simpler.
But it was 5:30 this morning when I first read your post and my brain was in low gear at the time. :)