Why don't SelectedIndices and SelectedItems work when ListView is created in unit test?

I am writing this question in the spirit of answering your own questions, as I have found a solution to the problem, but if someone has a better solution, I would love to listen to it.

In the application I'm currently working in, I will subclass the ListView control to add some functions, some of which interact with the ListView properties of SelectedIndices and SelectedItems.

The problem is that when I try to run a unit test, my subclass of the SelectedIndices and SelectedItems properties are not updated when I add items to the selection. I tried both

item.Selected = true

and

listView.SelectedIndices.Add(...)

But SelectedIndices or SelectedItems just don't show up. Unit tests for other parts of the functionality work fine.

unit test ListView?

+5
4

, SelectedIndices SelectedItems , ListView , MSDN ListViewItem.Selected:

, ListView (, TabControl , ). SelectedItems SelectedIndices ListView - .

, ListView . SelectedIndices SelectedItems .

- :

    [Test]
    public void CanGetSelectedItems()
    {
        // simple test to make sure that the SelectedIndices
        // property is updated
        using (var f = new DummyForm(listView))
        {
            f.Show();

            listView.SelectedIndices.Add(0);
            Assert.AreEqual(1, listView.SelectedIndices.Count);
        }
    }

    private class DummyForm : Form
    {
        public DummyForm(ListView listView)
        {
            // Minimize and make it not appear in taskbar to
            // avoid flicker etc when running the tests
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Controls.Add(listView);
        }
    }
+7

, :

listView.AccessibilityObject.ToString();// selecteditems refrehed

+8

, .CreateControl() ListView SelectedItems.

+2

" , .CreateControl() ListView SelectedItems."

, Multi-select.

0

All Articles