Solved

Trying to get a ListViewItem with a given value

Posted on 2006-07-02
3
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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