Solved

Trying to get a ListViewItem with a given value

Posted on 2006-07-02
3
422 Views
Last Modified: 2010-04-16
Hi,

I populated the ListView with driverLabel strings.  And now I'd like to get the ListViewItem with a given matching string.  But Items.IndexOf() return -1 even though I know it's been added.  Am I missing anything obvious?

Thanks,
Bob

string driverLabel = driverRun.Driver.DriverLabel;
if ( driverLabel != null && driverLV.Items.Count > 0 )
{
   int driverLabelIndex = driverLV.Items.IndexOf(new ListViewItem(driverLabel));
   if ( driverLabelIndex > -1 && driverLabelIndex < driverLV.Items.Count )
   {
      driverRunsLV.Items[driverRunIndex].Selected = true;
   }
}
0
Comment
Question by:ba272
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
aaronfeng earned 500 total points
ID: 17027251
Are you trying to select a specific item in the ListView?

string driverLabel = driverRun.Driver.DriverLabel;
if(!String.IsNullOrEmpty(driverLabel)) {
      foreach (ListViewItem item in this.listView1.Items.IndexOf()) {
            if (item.Text == driverLabel) {
                  item.Selected = true;
                  this.listView1.Select();
                  break;
            }
      }
 }

You might want to check my code, I wrote it free hand.

Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 
LVL 5

Expert Comment

by:aaronfeng
ID: 17027265
Oops, I already saw a typo, the for loop should be like this:

   foreach (ListViewItem item in this.listView1.Items)

Not

   foreach (ListViewItem item in this.listView1.Items.IndexOf())


Cheers,

Aaron
http://geekswithblogs.net/afeng/
0
 

Author Comment

by:ba272
ID: 17028178
thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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