Please login or register.

Login with username, password and session length

Author Topic: Visual Basic help - using a variable to access a Form Control  (Read 13763 times)

jtykal

  • Sr. Member
  • ****
  • Helpful Post Rating: 12
  • Posts: 86
  • It was working fine yesterday...
    • Casa Bella - Vacation Condo in Fort Myers, FL

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
« Last Edit: March 03, 2009, 03:02:14 PM by jtykal »
Logged
AHP-CM15A(ext.ant)-SmartMacros-OnAlert-DoorMonitor(custom)-Win7

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1340
  • He's just this guy. You know?
    • wgjohns.com
Re: Visual Basic help - using a variable to access a Form Control
« Reply #1 on: March 07, 2009, 03:06:42 AM »

Open up the VB help, and search the index for "control arrays".

I think this will tell you what you need to know.

 >!
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: Visual Basic help - using a variable to access a Form Control
« Reply #2 on: March 10, 2009, 06:34:03 AM »

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

Code: [Select]
        ' 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/
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: Visual Basic help - using a variable to access a Form Control
« Reply #3 on: March 10, 2009, 07:59:15 AM »

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.


Quote
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


« Last Edit: March 10, 2009, 08:02:36 AM by EL34 »
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

jtykal

  • Sr. Member
  • ****
  • Helpful Post Rating: 12
  • Posts: 86
  • It was working fine yesterday...
    • Casa Bella - Vacation Condo in Fort Myers, FL
Re: Visual Basic help - using a variable to access a Form Control
« Reply #4 on: March 10, 2009, 12:20:51 PM »

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! >!
Logged
AHP-CM15A(ext.ant)-SmartMacros-OnAlert-DoorMonitor(custom)-Win7

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: Visual Basic help - using a variable to access a Form Control
« Reply #5 on: March 10, 2009, 01:23:12 PM »

Quote
...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:

Quote
    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
« Last Edit: March 10, 2009, 03:03:46 PM by EL34 »
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

jtykal

  • Sr. Member
  • ****
  • Helpful Post Rating: 12
  • Posts: 86
  • It was working fine yesterday...
    • Casa Bella - Vacation Condo in Fort Myers, FL
Re: Visual Basic help - using a variable to access a Form Control
« Reply #6 on: March 10, 2009, 02:55:45 PM »

That's EXACTLY what I was looking for, EL34!  Thank you!  >!
Logged
AHP-CM15A(ext.ant)-SmartMacros-OnAlert-DoorMonitor(custom)-Win7

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: Visual Basic help - using a variable to access a Form Control
« Reply #7 on: March 10, 2009, 03:03:16 PM »

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. :)
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.