Link to home
Start Free TrialLog in
Avatar of rmmarsh
rmmarshFlag for United States of America

asked on

Problems with ListViewItem.FindItemWithText

I have a Listview that because of other processing, I loose track of what was selected.  I need to update that selection, so I tried FindItemWithText.  It isn't working the way I would expect it to.

The first item in the listview is called BookNbr and has an alphanumeric value.  I have verified the value I'm searching for, and it does indeed exist in the first column of the Listview.

Is there something I'm missing or misunderstanding?
ListViewItem lvi = dataBasePanel.FindItemWithText(tbBookNbr.Text);  //  find the item we were working on...

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pryrates
Pryrates
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
here is the full code assuming a listView1 on a Form1 and two Buttons (button1 and button2)
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            AddItems(10);
        }

        private void AddItems(int counter)
        {
            for (int i = 0; i < counter; i++)
            {
                ListViewItem item = new ListViewItem(string.Concat("item", listView1.Items.Count.ToString()));
                item.SubItems.Add("1234");
                listView1.Items.Add(item);            
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddItems(1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                MessageBox.Show(listView1.SelectedItems[0].Text);
            }
        }
    }

Open in new window

Avatar of rmmarsh

ASKER

The best 500 points I have ever awarded!

Thank you so very much!
You are welcome.
Glad I could assist :).