Important alert: (current site time 9/22/2014 5:52:57 AM EDT)
 

article

Select ListView Item Programmatically

Email
Submitted on: 4/10/2003 9:34:41 AM
By: Jason Heine 
Level: Beginner
User Rating: By 44 Users
Compatibility: VB.NET
Views: 163230
 
     This simple code will allow you to programmatically select an item in a listview. Hope this helps.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
	
		
		
		
	
	
		

How to select a ListView Item Programmatically.

 

This is a simple process, but most people forget a little step in doing this.

Step 1:

Create a ListView Item and name it to whatever you wish. I will use lv for simplicity.

Step 2:

Create a Button Item

Step 3:

Double Click the Button to start typeing code.

 

Step 4:

Enter the following code:

Private Sub Button1_Click(......) Handles Button1.Click

      lv.Items(0).Selected = True

      lv.Select() 'This is what most people forget. If you don't put this in here, it will never select the item in the list view.

End Sub

And that is it! Really Really simple, but very useful.

 

Step 5:

If you find this code useful. Vote for me.


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/28/2003 5:47:38 AM

Hi,

It is a short but powerful code. Being trying to do this for half a day but in vain. Decided to take a look at PSC and saw yours and it works perfectly. A 5 from me.

Hope can contact you. If possible can email me. Thx.

Regards
eddie


(If this comment was disrespectful, please report it.)

 
2/13/2004 9:16:55 AM

Thanks,
in many times it's not easy to find an answer for a simple question, but this guy was just what I was looking for.
(If this comment was disrespectful, please report it.)

 
9/26/2004 1:41:56 PM

Simple solution but it's worth very great! Initially I lost half a day in finding this.
(If this comment was disrespectful, please report it.)

 
10/11/2004 7:59:55 AMNitin Bagga

really helpful. Thanx
(If this comment was disrespectful, please report it.)

 
11/6/2004 6:40:21 PM

Good tip. Docs are horribly deficient on this simple task. Another way to solve this is to set the ListView.HideSelection property True, that is, if you also want the selection visible when the ListView does not have focus.
(If this comment was disrespectful, please report it.)

 
11/26/2004 10:35:35 AMMarceQ

Gracias, este código era lo que andaba buscando.
(If this comment was disrespectful, please report it.)

 
2/28/2005 8:32:49 PMChad Wilkins

at the risk of showing my ignorance.....where do you get the 'lv.select()' from never seen it heard of it or find it
(If this comment was disrespectful, please report it.)

 
2/28/2005 8:39:24 PMChad Wilkins

yep i showed it......your in vb.net.....im in vb 6.0 sorry
(If this comment was disrespectful, please report it.)

 
6/12/2005 3:39:42 AM

thx. i really missed out the Select() statement at the last line and this wasted me few hours!
(If this comment was disrespectful, please report it.)

 
7/28/2005 6:25:47 AMHappy Lobster

Thanks for submitting this simple code! It saved the day! Was scratching my head wondering why the item wasn't highlighting! Basic stuff!

I found that setting listview.HideSelection to False does not require the listview.select() command to be present!
(If this comment was disrespectful, please report it.)

 
6/19/2006 6:38:09 AMmethod

could show us how to select external listview item ?
(If this comment was disrespectful, please report it.)

 
8/1/2006 10:20:25 PMThammapat C.

simple but powerfull
(If this comment was disrespectful, please report it.)

 
10/17/2007 8:40:29 AM

I have few items in my list view.
when i type any letter in textbox ihe item starting with that letter should be selected. I got it and its selecting the word starting with typed letter. BUT that selected item should visible but i have to scroll down to see that item...........means the scroller should be scrolled so that selected item is visible.............how can we do it?
(If this comment was disrespectful, please report it.)

 
1/4/2008 4:49:58 AM

it's something like:
lv.TopItem = lv.Items[index_you_need]
(If this comment was disrespectful, please report it.)

 
7/13/2009 5:25:43 AMHagis

lv.Items(0).Selected = True
lv.Items(0).EnsureVisible
lv.Select()

will ensure that the listview is scrolled to a position to see what you've selected.

(If this comment was disrespectful, please report it.)

 
5/31/2010 9:57:14 AMjeremy450

Code doesnt work for me
(If this comment was disrespectful, please report it.)

 
11/9/2011 4:23:04 PMZePo

Thanks a lot !!! It worked perfectly. Just as you said, I wasn't calling the lv.Select() and I had already wasted a lot of time in other forums that didn't show all the code, so again, thanx a lot!!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.