• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Combining button.click events

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?
0
tiehaze
Asked:
tiehaze
  • 4
  • 2
  • 2
  • +1
2 Solutions
 
surajgupthaCommented:
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
0
 
tiehazeAuthor 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?
0
 
surajgupthaCommented:
I am not sure if i understand you. Can you explain?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tiehazeAuthor 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
0
 
SanclerCommented:
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
0
 
tiehazeAuthor Commented:
How do I set up the Select Case statement on the sender to determine which Listview it is referring to?
0
 
ColemssCommented:
cast the sender to a list view

Dim thisLV As ListView
thisLV  = sender as listview




0
 
SanclerCommented:
Like this?

        Dim ctl As Control = CType(sender, Control)
        Dim MoveUp As Boolean = False
        Dim thisLV As ListView

        Select Case ctl.Name
            Case "btnUpAssets"
                MoveUp = True
                thisLV = AssetsLV
            Case "btnDownAssets"
                MoveUp = False
                thisLV = AssetsLV
            Case "btnUpLiabilities"
                MoveUp = True
                thisLV = LiabilitiesLV
            Case "btnDownLiabilities"
                MoveUp = False
                thisLV = LiabilitiesLV
            'etc
        End Select

Roger
0
 
tiehazeAuthor 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?
0
 
ColemssCommented:
I see the problem. You are trying to cast the button events to a listview .  You can't.

  Dim ctl As Button = CType(sender, Button)
        Dim MoveUp As Boolean = False
        Dim thisLV As ListView

        Select Case ctl.Name  :  this may be button.name or text
            Case "btnUpAssets"
                MoveUp = True
                thisLV = AssetsLV
            Case "btnDownAssets"
                MoveUp = False
                thisLV = AssetsLV
            Case "btnUpLiabilities"
                MoveUp = True
                thisLV = LiabilitiesLV
            Case "btnDownLiabilities"
                MoveUp = False
                thisLV = LiabilitiesLV
            'etc
        End Select

then do your work with ThisLV
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

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