Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > ListView...select programmatically


Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2005, 06:35 AM
ronaldlangi ronaldlangi is offline
Regular
 
Join Date: Dec 2004
Posts: 74
Default ListView...select programmatically


I have a ListView control and I want to programmatically select an Item inside the ListView and thus highlighting the Item too, just as if it was selected by mouse.

I suspect that i have to indicate the index of the Item but I don't know how to highlight the item in the box. Please help...

Thanks.
Reply With Quote
  #2  
Old 12-05-2005, 12:34 AM
jo0ls jo0ls is offline
Senior Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2005
Location: London
Posts: 1,050
Default

You need the listviewitem itself to use listview.items.indexof, you can't just search by name. To get the listviewitem you just loop through all listview items in the listview with a for each ... in ... next loop:
Code:
' there's a form with listview1 and button1 on it... Dim randomGenerator As New Random Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListView1.View = View.Details ListView1.Columns.Add("Name", 100, HorizontalAlignment.Left) ListView1.MultiSelect = False For i As Integer = 0 To 15 ListView1.Items.Add("Item " & i) Next End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer = randomGenerator.Next(0, 16) ' search for a random item 'loop through each item in the listview For Each lvi As ListViewItem In ListView1.Items() ' test to see if it is our item If lvi.Text = "Item " & i Then ' if it is then select it lvi.Selected = True Else ' if listview1.multiselect were = false then you could deselect any other selections here End If Next ' the button is selected so you wont see anything: ListView1.Select() End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->