Solved

Trying to get a ListViewItem with a given value

Posted on 2006-07-02
3
417 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Linq help 1 16
.NET Error 7 42
Windows Service with UDP 2 28
XML & .net 5 20
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now