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(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles btnUpAssets.Click, btnDownAssets.Click
If AssetsLV.SelectedIndices.C ount = 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(newI ndex, item).Selected = True
AssetsLV.Focus()
End Sub
These next two buttons are relating to the listview 'LiabilitiesLV'
Private Sub MoveButtonLiabilities_Clic k(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpLiabilities.Click, btnDownLiabilities.Click
If LiabilitiesLV.SelectedIndi ces.Count = 0 Then Exit Sub
Dim item As ListViewItem = LiabilitiesLV.SelectedItem s(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?
These first two buttons are relating to the listview 'AssetsLV'
Private Sub MoveButtonAssets_Click(ByV
If AssetsLV.SelectedIndices.C
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(newI
AssetsLV.Focus()
End Sub
These next two buttons are relating to the listview 'LiabilitiesLV'
Private Sub MoveButtonLiabilities_Clic
If LiabilitiesLV.SelectedIndi
Dim item As ListViewItem = LiabilitiesLV.SelectedItem
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
LiabilitiesLV.Items.Insert
LiabilitiesLV.Focus()
End Sub
Can I combine the two somehow?
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?
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
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.Cou nt = 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(newInd ex, 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
Dim thisLV As ListView
Dim MoveUp As Boolean
'see comment below
If thisLV.SelectedIndices.Cou
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(newInd
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
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
Dim thisLV As ListView
thisLV = sender as listview
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am getting the error:
{"Unable to cast object of type 'System.Windows.Forms.Tool StripButto n' to type 'System.Windows.Forms.Cont rol'."}
on line:
Dim ctl As Control = CType(sender, Control)
Any ideas?
{"Unable to cast object of type 'System.Windows.Forms.Tool
on line:
Dim ctl As Control = CType(sender, Control)
Any ideas?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if sender is btnUpAssets or sender is btnDownAssets
--Paste the lines in first sub
else
--paste the lines in second sub