Solved

How do I ensure an item is selected in a ListView?

Posted on 2009-05-05
3
237 Views
Last Modified: 2013-12-26
I have a listview and I want to ensure that an item is always selected.  If a user clicks in a blank area of the listview, I want the current selection to remain selected.  I have some code below that does the job, but it seems overly complicated.  Is there an easier way to do this?
Private p_lvwItem As ListViewItem = Nothing
 

Private Sub lvwList_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvwList.ItemSelectionChanged 

  If Not e.IsSelected Then 

    p_lvwItem = e.Item 

  End If 

End Sub 
 

Private Sub lvwList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwList.MouseDown 

  lvwList.BeginUpdate() 

End Sub 
 

Private Sub lvwList_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwList.MouseUp 

  If lvwList.SelectedItems.Count = 0 Then 

    lvwList.Items.Item(p_lvwItem.Index).Selected = True 

  End If 

  lvwList.EndUpdate() 

End Sub

Open in new window

0
Comment
Question by:appsysdco
3 Comments
 
LVL 12

Accepted Solution

by:
Krys_K earned 500 total points
Comment Utility
HI There
I'm not sure there is a better way to do this other than what you have. Given your criteriea that if any of the ListView object itself is clicked and is blank.
If your criteria was a little different, such as If the Listview has lost the focus and nothing is selected then select an item; you could use the LostFocus Event to do the same thing instead.
Regards
Krystian
0
 

Author Comment

by:appsysdco
Comment Utility
Thanks, I wasn't sure if there was going to be an easier way or not.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now