Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Withevents for dynamicly created controls

Withevents for dynamicly created controls

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Hovey
    wrote on last edited by
    #1

    Hi there! I need some help with events for dynamicly created objects. The code below illustrates the problem. My situation will be a little different since the objects I will be dynamicly creating will be custom, but the problem is the same. The event will only fire on the last created object. How can I have an event fired within the form Class each time an individual instance of the label is clicked??

    Public Class Form1
    Friend WithEvents lbl As Label
    Private i As Integer

    Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        i = 1
    End Sub
    
    Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        lbl = New Label
    
        lbl.Name = "lbl" & i
        lbl.Text = "Label " & i
        lbl.Location = e.Location
        lbl.AutoSize = True
    
        Me.Controls.Add(lbl)
        i += 1
    
    End Sub
    
    Private Sub lbl\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl.Click
    
        MsgBox(lbl.Text)
    
    End Sub
    

    End Class

    A D 2 Replies Last reply
    0
    • D David Hovey

      Hi there! I need some help with events for dynamicly created objects. The code below illustrates the problem. My situation will be a little different since the objects I will be dynamicly creating will be custom, but the problem is the same. The event will only fire on the last created object. How can I have an event fired within the form Class each time an individual instance of the label is clicked??

      Public Class Form1
      Friend WithEvents lbl As Label
      Private i As Integer

      Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          i = 1
      End Sub
      
      Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
          lbl = New Label
      
          lbl.Name = "lbl" & i
          lbl.Text = "Label " & i
          lbl.Location = e.Location
          lbl.AutoSize = True
      
          Me.Controls.Add(lbl)
          i += 1
      
      End Sub
      
      Private Sub lbl\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl.Click
      
          MsgBox(lbl.Text)
      
      End Sub
      

      End Class

      A Offline
      A Offline
      Anshumas
      wrote on last edited by
      #2

      you must read about delegates in .net....

      Anshuman Singh

      1 Reply Last reply
      0
      • D David Hovey

        Hi there! I need some help with events for dynamicly created objects. The code below illustrates the problem. My situation will be a little different since the objects I will be dynamicly creating will be custom, but the problem is the same. The event will only fire on the last created object. How can I have an event fired within the form Class each time an individual instance of the label is clicked??

        Public Class Form1
        Friend WithEvents lbl As Label
        Private i As Integer

        Private Sub Form1\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            i = 1
        End Sub
        
        Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
            lbl = New Label
        
            lbl.Name = "lbl" & i
            lbl.Text = "Label " & i
            lbl.Location = e.Location
            lbl.AutoSize = True
        
            Me.Controls.Add(lbl)
            i += 1
        
        End Sub
        
        Private Sub lbl\_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbl.Click
        
            MsgBox(lbl.Text)
        
        End Sub
        

        End Class

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        This code won't work. You only have one label wired up with it. Basically, you can't use WithEvents on a class-level variable and expect this to work. Drop the "Friend WithEvents..." line completely. Your MouseUp code has to create the label and wire up the events itself. This way, every label you create will maintain event connections.

        Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
            Dim lbl As New Label
            ' lbl.Name = "lbl" & i  ' Drop this line.  It's not necessary
            lbl.Text = "Label " & i
            lbl.Location = e.Location
            lbl.AutoSize = True
            AddHandler lbl.Click, AddressOf lbl\_Click
            Me.Controls.Add(lbl)
            i += 1
        End Sub
        

        and drop the "Handles lbl.Click" from the end of the Sub you defined.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        D 1 Reply Last reply
        0
        • D Dave Kreskowiak

          This code won't work. You only have one label wired up with it. Basically, you can't use WithEvents on a class-level variable and expect this to work. Drop the "Friend WithEvents..." line completely. Your MouseUp code has to create the label and wire up the events itself. This way, every label you create will maintain event connections.

          Private Sub Form1\_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
              Dim lbl As New Label
              ' lbl.Name = "lbl" & i  ' Drop this line.  It's not necessary
              lbl.Text = "Label " & i
              lbl.Location = e.Location
              lbl.AutoSize = True
              AddHandler lbl.Click, AddressOf lbl\_Click
              Me.Controls.Add(lbl)
              i += 1
          End Sub
          

          and drop the "Handles lbl.Click" from the end of the Sub you defined.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008

          D Offline
          D Offline
          David Hovey
          wrote on last edited by
          #4

          Works great! thanks for your help.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups