Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by Doggo120
  • Leaders
Posted

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

[sIGPIC]e[/sIGPIC]
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...