Reference to Run-Time Objects

Doggo120

Newcomer
Joined
Oct 7, 2011
Messages
23
Location
Colombia
Hi again :P

In my musical project, I'm creating buttons and labels at runtime. I was wondering how can I make changes to a certain button object via a label object, like this:

Dim Label1 as New Label
Dim Button1 as New Button

Me.Controls.Add(Label1)
Me.Controls.Add(Button1)

AddHandler Label1.MouseEnter, AddressOf MousyEnter

Private Sub MousyEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)

' This is what I want to do:
"Button1".Text= "What's up!"
' Can I access Button1 within Me.Controls?

End Sub

I cannot seem to figure this out. Any suggestions please let me know!
Thanks!!!
 
Last edited:
It looks like your code listing is incomplete. The issue is probably "scope". Most likely, your are declare the button and label variables as locals (i.e. they belong to a sub or function), when what you really want are fields (they belong to the class).

Code:
Class Form1 Inherits Form
    Sub New()
        Dim Label1 as New Label
        Dim Button1 as New Button

        Me.Controls.Add(Label1)
        Me.Controls.Add(Button1)

        AddHandler Label1.MouseEnter, AddressOf MousyEnter
    End Sub 

    Private Sub MousyEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Button1.Text = "What's up!" ' [COLOR="Red"]ERROR: MousyEnter does not know what Button1 is.[/COLOR]
    End Sub
End Class
In this case, the variables Button1 and Label1 belong to Sub New. This is the scope of the variables. They have local scope (local to Sub New) and are referred to as "locals." They can only be accessed from Sub New. You can change it like so:
Code:
Class Form1 Inherits Form
[COLOR="Blue"]    Dim Label1 as New Label
    Dim Button1 as New Button
[/COLOR]
    Sub New()
        Me.Controls.Add(Label1)
        Me.Controls.Add(Button1)

        AddHandler Label1.MouseEnter, AddressOf MousyEnter
    End Sub 

    Private Sub MousyEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Button1.Text= "What's up!" 
    End Sub
End Class
Now Button1 and Label1 are declared outside Sub New, so they belong to the whole class. The variables have class scope. You can access them anywhere inside Form1.


You can also access controls without using variables at all, but you must give the control a name.

Code:
Class Form1 Inherits Form
    Dim Label1 as New Label
    Dim Button1 as New Button

    Sub New()
[COLOR="Green"]        ' Button1 would probably be a more appropriate name, but it can be anything you want
[/COLOR]      [COLOR="Blue"]  Button1.Name = "LeButton"[/COLOR]

        Me.Controls.Add(Label1)
        Me.Controls.Add(Button1)

        AddHandler Label1.MouseEnter, AddressOf MousyEnter
    End Sub 

    Private Sub MousyEnter(ByVal sender As System.Object, ByVal e As System.EventArgs)
[COLOR="Blue"]        Controls("LeButton").Text= "What's up!" 
[/COLOR]    End Sub
End Class
 
Yeah! Thanks, I really missed the "scope" you mentioned. The buttons and labels are created inside a Sub procedure, like this:

Public Sub CreateButton&Label(ByVal Number as Integer)

For a as Integer = 0 to Number

dim Button1 as New Button
dim Label1 as New Label
' Blah blah blah...

Next a

End Sub

But perharps the .Name property will solve my problems!
Thank you again snarfblam, you're like a super programming sensei :D
 
Back
Top