Doggo120 Posted October 8, 2011 Posted October 8, 2011 (edited) 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!!! Edited October 8, 2011 by Doggo120 Quote
Leaders snarfblam Posted October 8, 2011 Leaders Posted October 8, 2011 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). 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: 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. 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 Quote [sIGPIC]e[/sIGPIC]
Doggo120 Posted October 9, 2011 Author Posted October 9, 2011 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.