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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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.
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.)
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.)
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.)
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.)
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.)
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.)