Problems with ListViewItem.FindItemWithText

rmmarsh
rmmarsh used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
if you set the "HideSelection" Propertyoft the ListView to "false" the selection is not removed - even if the control looses focus.
I other words: the selection is saved.


ListViewUpdateKeepSelection.zip

Commented:
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

Author

Commented:
The best 500 points I have ever awarded!

Thank you so very much!

Commented:
You are welcome.
Glad I could assist :).

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial