none
Getting the index number from dynamic array RRS feed

  • Question

  • I created a handle for a dynamically created array of ovalshapes.

            Try
                For number As Int32 = 1 To frmMainMenu.DataGridView1.RowCount
                    AddHandler PadArray(number).Click, AddressOf _PadClick
                Next
            Catch ex As Exception
                Beep()
            End Try

     The event trigger if I click any of the ovalshapes. My problem is to once the even is trigger I need to know the index number of the array. Is there a way to do this?

        Private Sub _PadClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim Pad As OvalShape
            Dim PadIndex As Integer

              PadIndex ???

        End Sub


    Ros

    Wednesday, January 23, 2019 11:38 PM

Answers

  • Best thought is to set the Tag property to a integer value then integrate the Tag property e.g. 

    Dim PadIndex As Integer = Cint(CType(sender, OvalShape).Tag)

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Alex Li-MSFT Thursday, January 24, 2019 2:09 AM
    • Marked as answer by rosvb Thursday, January 24, 2019 2:22 PM
    Wednesday, January 23, 2019 11:52 PM
    Moderator

All replies

  • Best thought is to set the Tag property to a integer value then integrate the Tag property e.g. 

    Dim PadIndex As Integer = Cint(CType(sender, OvalShape).Tag)

     

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Alex Li-MSFT Thursday, January 24, 2019 2:09 AM
    • Marked as answer by rosvb Thursday, January 24, 2019 2:22 PM
    Wednesday, January 23, 2019 11:52 PM
    Moderator
  • Hi,

    You can use Dictionary

    Public Class Form1
        Dim pad As New Dictionary(Of ovalshape, Integer)
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            pad.Add(ovalshape1, 1)
            pad.Add(ovalshape2, 2)
            pad.Add(ovalshape3, 3)
            ...
            For i = 0 To pad.Count - 1
                AddHandler CType(pad.Keys(i), ovalshape).Click, AddressOf _PadClick
            Next
        End Sub
        Private Sub _PadClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim PadIndex As Integer = pad(CType(sender, ovalshape))
            MsgBox(PadIndex)
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 24, 2019 2:14 AM
  • All good. Thanks

    Ros

    Thursday, January 24, 2019 2:23 PM