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

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

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
0
David Bird
Asked:
David Bird
  • 3
  • 2
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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

0
 
Robert SchuttSoftware EngineerCommented:
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...
0
 
David BirdPartnerAuthor Commented:
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
0
 
Robert SchuttSoftware EngineerCommented:
Don't apologize, there's more than 1 way to do things and I'll gladly explore the options with you. This was just the first thing that occurred to me but there may be a better way.

First, re-reading the code I noticed something that I hadn't before: searching the button in the list is a bit of a roundabout because the 'sender' is that same button! So you could just do this:
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As  _
       System.EventArgs)
        Dim btn As Button = CType(sender, System.Windows.Forms.Button)
        If MessageBox.Show("you have clicked: '" & CType(btn.Text, String) & "' - delete it?", "Confirm deletion", MessageBoxButtons.OKCancel) = DialogResult.OK Then
            HostForm.Controls.Remove(btn)
            Me.List.Remove(btn)
        End If
    End Sub

Open in new window

Another way you asked about would be to search a button by Text, you could define a new method for that:
    Public Sub RemoveByText(ByVal pm_Text As String)
        Dim btn As Button = Nothing
        For Each btnIterator As Button In Me.List
            If btnIterator.Text = pm_Text Then
                btn = btnIterator
                Exit For
            End If
        Next
        ' Check to be sure there is a button to remove.
        If btn IsNot Nothing Then
            HostForm.Controls.Remove(btn)
            Me.List.Remove(btn)
        End If
    End Sub

Open in new window

0
 
David BirdPartnerAuthor Commented:
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
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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