Link to home
Start Free TrialLog in
Avatar of David Bird
David BirdFlag for United States of America

asked on

Getting Index position of an array from the clickhandler

I have some class code I (scarfed) that allows me to create dynamic control buttons on a form array.  The code works. It's code from MS help and I've attached it.  I need to be able to remove a button by it's index location when it hits the eventhandler "clickHandler".
I'm just over beginner in programming here so again, be kind please.  I've expanded all methods and properties I know how to do in debugger to see what is received as sender and e objects.  How do I find the index of the item sent to the handler please?

thanks for any thoughts and help.
dynamicButtons.txt
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Try this:
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As  _
       System.EventArgs)

        If MessageBox.Show("you have clicked button " & CType(CType(sender,  _
           System.Windows.Forms.Button).Tag, String) & " - delete it?", "Confirm deletion", MessageBoxButtons.OKCancel) = DialogResult.OK Then
            Dim n As Integer = CType(CType(sender, System.Windows.Forms.Button).Tag, Integer)
            ' Remove the n-th button added to the array from the host form 
            ' controls collection. Note the use of the default property in 
            ' accessing the array.
            HostForm.Controls.Remove(Me(n - 1))
            Me.List.RemoveAt(n - 1)
        End If
    End Sub

Open in new window

That piece of code works because the original code sets:
aButton.Tag = Me.Count

Open in new window

Uhm, by the way, you may want to add some logic to decrease the .Tag of the remaining buttons (with a .Tag greater than 'n') because the List is getting smaller all the time and the numbers don't match up anymore...
Avatar of David Bird

ASKER

I see your point.  The tag is equivalent - 1 to the index in the array until I delete an item.  
Instead of using the .tag as the reference to the index to remove, is there not a way to search for the button text or some unique value in the button's properties to return its current Index or indexOf?

Or is it smarter to do as you say above and rebuild from the tag on down and/or the array when a row in the array is deleted?

I apologize for the remedial questions, but your response(s) are/have been valuable. I think I must be missing something simple as a method to find or a property sent that has the index value?

thanx
daver
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As much as I would like to continue asking different questions, you've answered my question perfectly. All points awared sir.  I'm now "attempting code to adjust locations of the buttons based on the button removed. Based on your responses, I think I can git'r dun! <br /><br />Thank you so kindly for helping me. You da man! <br />daver