Link to home
Start Free TrialLog in
Avatar of tiehaze
tiehaze

asked on

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?
Avatar of surajguptha
surajguptha
Flag of United States of America image

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
Avatar of tiehaze
tiehaze

ASKER

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?
Avatar of tiehaze

ASKER

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
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
Avatar of tiehaze

ASKER

How do I set up the Select Case statement on the sender to determine which Listview it is referring to?
cast the sender to a list view

Dim thisLV As ListView
thisLV  = sender as listview




ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

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
Avatar of tiehaze

ASKER

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?
SOLUTION
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