• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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