determining which listview has selected items

tooess

Newcomer
Joined
Aug 16, 2005
Messages
3
I have a form which has two listviews on it. each display different information. I also have a button which when it is clicked i want it to determine if any thing is selected in either listview. I cant figure out how to do this so any help would be appreciated.

Thanks in advance.
 
Not quite

Not quite, as there are two listviews. The program must determine which one has the selected item before it can get the item.

Thanks anyway
 
Couldn't both listboxes have a selected item though? The UI will allow you to select an item in each listbox before clicking the button.
If you want to see if an item is selected in a particular listbox just check to see if it's SelectedItem is nothing (or null if C#).
 
As Plausibly stated, its possible and expected that the end user will probably select an item from both boxes. If your trying to make it so that the user can only select from either view, then set a handler for the SelectedIndexChanged event. If the user selects an item from one of the list views then set the Selectedindex of the other box to -1

-=Simcoder=-
 
Hi Plausibly, I tried that but it seems that by default the top most item in a listview is classed as the selected item whether it is actually highlighted or not. so is there a way to determine if a listview has a highlighted item?
 
Sorry, I was getting your listview confused with a listbox. Anyways, you can check to see what items are selected or checked by doing something of this sort.

Visual Basic:
 For Each Item In lvMain.Items

       If Item.Checked = True Then
       'Do Something
       End If

       If Item.Selected = True Then
       'Do Something
       End If

Next

-=Simcoder=-
 
This might work in most circumstances, even if the end-user has selected items in both ListViews:

Code:
[COLOR=Green]'This code assumes that both ListView1 and ListView2 belong to 
'the Controls collection of Form1 (could be a Form, Panel, etc).[/COLOR]
[COLOR=Blue]Dim[/COLOR] alItems [COLOR=Blue]As New [/COLOR] ArrayList

[COLOR=Blue]For Each [/COLOR] ctrl [COLOR=Blue]As[/COLOR] Control [COLOR=Blue]In[/COLOR] Form1.Controls
  [COLOR=Blue]Select Case [/COLOR] ctrl.Name
    [COLOR=Blue]Case[/COLOR] ListView1.Name
      alItems.AddRange(ListView1.[url=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformslistviewclassselecteditemstopic.asp=]SelectedItems[/url]) 
    [COLOR=Blue]Case[/COLOR] ListView2.Name
      alItems.AddRange(ListView2.SelectedItems)
    [COLOR=Blue]Case Else[/COLOR]
      'take default action
  [COLOR=Blue]End Select
Next[/COLOR]
Another option is to use the ListView's SelectedIndexChanged event to manage item selection in both ListView controls.
Code:
[COLOR=Blue]Private Sub [/COLOR] ListView1_SelectedIndexChanged([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] ListView1.SelectedIndexChanged
  [COLOR=Blue]Dim[/COLOR] objItem [COLOR=Blue]As[/COLOR] Object
  objItem = ListView1.SelectedItem

  [COLOR=Green]'OR[/COLOR]

  [COLOR=Blue]Dim[/COLOR] item [COLOR=Blue]As[/COLOR] ListViewItem
  item = ListView1.SelectedItem

  [COLOR=Green]'Use this if ListView event handler is shared[/COLOR]

  [COLOR=Blue]Dim[/COLOR] ctrl [COLOR=Blue]As[/COLOR] Control
  ctrl = item.Parent
  [COLOR=Blue]Dim[/COLOR] listViewName [COLOR=Blue]As[/COLOR] String = ctrl.Name
[COLOR=Blue]End Sub[/COLOR]
 
Back
Top