[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

Finding what element an object array is

Hi,
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!
Thanks
0
jellydeal
Asked:
jellydeal
  • 2
  • 2
1 Solution
 
newyuppieCommented:
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.

NY
0
 
jellydealAuthor Commented:
Hi,

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

thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
            Me.Controls.Add(pnl)
            panels.Add(pnl)
        Next
    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

PanelClick.jpg
0
 
jellydealAuthor Commented:
Great Example,

Thanks for your time!
Kindest regards
0
 
newyuppieCommented:
Hi IdleMind, very elegant indeed.. thanks for that example.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now