Finding what element an object array is

Posted on 2009-04-18
Last Modified: 2012-05-06
I have various panels on my form created at runtime. All are part of an array. Dim panel(x) as new panel etc.
I am trying to find out if I can get the element of that array by clicking on it.
Ie can I find out each panels array element by clicking on it?
Hope this makes sense!
Question by:jellydeal
    LVL 13

    Expert Comment

    Maybe you can use the .Tag property when creating the panels, so that you assign a unique tag to each one.

    After, when you click on the form, run a loop through the array and check for the same tag.


    Author Comment


    can you post a simple example?
    I cant find anything relating to tags.

    LVL 85

    Accepted Solution

    Here is an example without using the Tag() Property:
    Public Class Form1
        Private panels As New List(Of Panel)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For i As Integer = 1 To 5
                Dim pnl As New Panel
                pnl.Size = New Size(25, 25)
                pnl.BorderStyle = BorderStyle.FixedSingle
                pnl.Location = New Point(i * 25, 25)
                AddHandler pnl.Click, AddressOf pnl_Click
        End Sub
        Private Sub pnl_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim pnl As Panel = CType(sender, Panel)
            Dim index As Integer = panels.IndexOf(pnl)
            Label1.Text = "You clicked on Panel Index #" & index
        End Sub
    End Class

    Open in new window


    Author Closing Comment

    Great Example,

    Thanks for your time!
    Kindest regards
    LVL 13

    Expert Comment

    Hi IdleMind, very elegant indeed.. thanks for that example.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now