Referring to a run-time generated object?-VBForums
Results 1 to 4 of 4

Thread: Referring to a run-time generated object?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Referring to a run-time generated object?

    Hi all, I have a button on my form which can generate an unlimited number of panels.

    Code:
     Dim pnl As New Panel With {.BorderStyle = BorderStyle.FixedSingle, .BackColor = Color.Gainsboro, .Tag = p}
                If ProcessPanels.Count = 0 Then
                    pnl.Bounds = New Rectangle(12, 45, 900, 500)
                Else
                    pnl.Bounds = New Rectangle(ProcessPanels(ProcessPanels.Count - 1).Right + 12, 45, 900, 500)
                End If
    Code:
     Me.Controls.Add(pnl)
    How do I refer to this in a private sub (baring in mind that I have an unlimited amount that can be created and need to be able to refer to each independently, as well as, in some cases, refer to all at the same time.

    Specifically, I want to basically do this:

    Code:
    Private Sub pnl_MouseEnter(sender As Object, e As EventArgs) Handles pnl.MouseEnter
    Obviously the above does not work as the object does not exist when I run the application (yet).

    Thanks very much for the help!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,276

    Re: Referring to a run-time generated object?

    Get rid of the Handles clause on that event handler method but everything else will be the same. When you create the Panel, register the event handler like this:
    vb.net Code:
    1. AddHandler pnl.MouseEnter, AddressOf pnl_MouseEnter
    Inside the event handler, you can get access to the Panel that raised the event via the 'sender' parameter:
    vb.net Code:
    1. Dim pnl = DirectCast(sender, Panel)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    75

    Re: Referring to a run-time generated object?

    Had the same solution as jmcilhinney ,

    but got out posted when i was typing the solution...
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  4. #4
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    737

    Re: Referring to a run-time generated object?

    If you create the event handler, minus the Handles bit you can always connect it up at runtime. e.g.

    Code:
    Private Sub pnl_MouseEnter(sender As Object, e As EventArgs)
    dim pnl as Panel = DirectCast(sender, Panel)
    'whatever
    End Sub
    Code:
    Dim pnl As New Panel With {.BorderStyle = BorderStyle.FixedSingle, .BackColor = Color.Gainsboro, .Tag = p}
                If ProcessPanels.Count = 0 Then
                    pnl.Bounds = New Rectangle(12, 45, 900, 500)
                Else
                    pnl.Bounds = New Rectangle(ProcessPanels(ProcessPanels.Count - 1).Right + 12, 45, 900, 500)
                End If
    
    AddHandler pnl.MouseEnter, AddressOf pnl_MouseEnter
    When the event handler fires, the sender argument will refer to the panel that triggered the event, you would just need to cast it to a panel object to use it.


    Edit: Two people type quicker than me it would seem...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width