Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

Its easy to do, If you want to get grab the selected item, you can do this

 

lstMain.SelectedItem

 

-=Simcoder=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

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

  • Administrators
Posted

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#).

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted
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?
Posted

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.

 


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=-

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

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

 

[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.

[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]

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...