By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

how to use a dynamically generated button to change the text of a label created at de

P: 1
Hello pro's, i created a label (label1) and a button (button1) at design time. When the button is clicked, the text of the label changes to ''button1 disposed", button1 is disposed and a dynamic button is generated. The event handler for the dynamically generated button is supposed to do what button1 did but in the event handler procedure for the dynamic button, it says that label1 is not declared. Pls i seriously need help.

Here is my code.

Expand|Select|Wrap|Line Numbers
  1. Public Sub buttn_Click()
  2. label1.Text = "new button clicked"
  3. buttn.Dispose()
  4. ' this code is kept in a module called EventHandlers.
  5. ' it says label1.Text and buttn are not declared.
  6. ' but if i put it in the forms class, the error goes
  7. ' i want it to b in a Module!
  8.  
  9. End Sub
  10.  
  11.  
  12. Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
  13.  
  14. label1.Text = "button1 disposed"
  15. button1.Dispose()
  16.  
  17. Dim buttn As New Button()
  18. buttn.Location = New Point (69, 284)
  19. buttn.Text = "New button"
  20. Me.Controls.Add(buttn)
  21. AddHandler buttn.click, AddressOf buttn_Click
  22.  
  23. End Sub
Aug 12 '13 #1
Share this Question
Share on Google+
1 Reply


100+
P: 158
Hey EllahV, i don't know why you're getting an error message for 'label1' because you didn't shown the declaration part of it. But regarding 'buttn', the scope/life lies only inside the 'button1_Click' event method, you can't access it from the outside of it.
Aug 16 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.