We help IT Professionals succeed at work.

New podcast episode! Our very own Community Manager, Rob Jurd, gives his insight on the value of an online community. Listen Now!

x

Combining button.click events

222 Views
Last Modified: 2013-11-27
I was wondering how I could somehow combine the 4 button click events:

These first two buttons are relating to the listview 'AssetsLV'

Private Sub MoveButtonAssets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpAssets.Click, btnDownAssets.Click
        If AssetsLV.SelectedIndices.Count = 0 Then Exit Sub
        Dim item As ListViewItem = AssetsLV.SelectedItems(0)
        Dim newIndex As Integer = item.Index + IIf(sender Is btnUpAssets, -1, 1)
        If (sender Is btnDownAssets And item.Index = AssetsLV.Items.Count - 1) _
        OrElse (sender Is btnUpAssets And item.Index = 0) Then
            Exit Sub
        End If
        AssetsLV.Items.Remove(item)
        AssetsLV.Items.Insert(newIndex, item).Selected = True
        AssetsLV.Focus()
    End Sub

These next two buttons are relating to the listview 'LiabilitiesLV'

    Private Sub MoveButtonLiabilities_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpLiabilities.Click, btnDownLiabilities.Click
        If LiabilitiesLV.SelectedIndices.Count = 0 Then Exit Sub
        Dim item As ListViewItem = LiabilitiesLV.SelectedItems(0)
        Dim newIndex As Integer = item.Index + IIf(sender Is btnUpLiabilities, -1, 1)
        If (sender Is btnDownLiabilities And item.Index = LiabilitiesLV.Items.Count - 1) _
        OrElse (sender Is btnUpLiabilities And item.Index = 0) Then
            Exit Sub
        End If
        LiabilitiesLV.Items.Remove(item)
        LiabilitiesLV.Items.Insert(newIndex, item).Selected = True
        LiabilitiesLV.Focus()
    End Sub

Can I combine the two somehow?
Comment
Watch Question

Private Sub MoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpAssets.Click, btnDownAssets.Click,btnUpLiabilities.Click, btnDownLiabilities.Click
if sender is btnUpAssets or sender is btnDownAssets
--Paste the lines in first sub
else
--paste the lines in second sub

Author

Commented:
Is there anyway to tag the buttons with the ListView name it is related to, and then convert the tag name to the listview name?
I am not sure if i understand you. Can you explain?

Author

Commented:
K, well in my question, I stated that there are only 4 buttons, and two listboxes. There are actually 12 buttons and 6 listboxes. If I do what you said with:

if sender is btnUpAssets or sender is btnDownAssets
--Paste the lines in first sub
Elseif ...
--paste the lines in second sub
Elseif ...
--paste the lines in third sub
Elseif ...
--paste the lines in fourth sub
Elseif ...
--paste the lines in fifth sub
Elseif ...
--paste the lines in sixth sub
End if

I would like to condense it all so that I only have to have one set of code, where the ListView is a variable based on the buttons clicked. If not, then if I need to update the code, I have to do it in all six places

Commented:
Generalise the sub like this

        Dim thisLV As ListView
        Dim MoveUp As Boolean

        'see comment below

        If thisLV.SelectedIndices.Count = 0 Then Exit Sub
        Dim item As ListViewItem = thisLV.SelectedItems(0)
        Dim newIndex As Integer = item.Index + IIf(MoveUp, -1, 1)
        If ((Not MoveUp) And item.Index = ThisLV.Items.Count - 1) _
        OrElse (MoveUp And item.Index = 0) Then
            Exit Sub
        End If
        ThisLV.Items.Remove(item)
        ThisLV.Items.Insert(newIndex, item).Selected = True
        ThisLV.Focus()

And then in the button click use a Select Case statement on the sender to set thisLV and MoveUp appropriately.  You could do this either in the existing sub at the point marked with the comment above; or make this a separate sub and pass the appropriate listview and direction values in as its arguments from the click event.

Roger

Author

Commented:
How do I set up the Select Case statement on the sender to determine which Listview it is referring to?

Commented:
cast the sender to a list view

Dim thisLV As ListView
thisLV  = sender as listview




Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I am getting the error:

{"Unable to cast object of type 'System.Windows.Forms.ToolStripButton' to type 'System.Windows.Forms.Control'."}

on line:

 Dim ctl As Control = CType(sender, Control)

Any ideas?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.