Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-05
3
Medium Priority
?
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Accepted Solution

by:
Krys_K earned 1500 total points
ID: 24310917
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
ID: 24314769
Thanks, I wasn't sure if there was going to be an easier way or not.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

604 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