Does Anybody Here Know C#?

closed account (zb0S216C)
If you don't have knowledge of C#, why're you reading this? If you do have knowledge of C#, then maybe you can help me.

Here's the problem:
I'm trying to get the selected items of a ListView with Contains( ), and IndexOf( ). However, I don't know how to use them. Also, MSDN doesn't help all that much( doesn't provide examples of Contains( ), and IndexOf( ) ).


Here's my code so far( only the relevant bits ):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void listView1_SelectedIndexChanged( object sender, EventArgs e )
        {
            if( listView1.Items.Count <= 3 )
            {
                for( int iLoop = 0; iLoop < listView1.Items.Count; iLoop++ )
                {
                    if( listView1.Items[ iLoop ].Selected )
                    {
                        RB4WD.Checked = _CarData[ iLoop ]._bDrive_type[ 0 ];
                        RBFWD.Checked = _CarData[ iLoop ]._bDrive_type[ 1 ];
                        RBRWD.Checked = _CarData[ iLoop ]._bDrive_type[ 2 ];
                        NameBox.Text = _CarData[ iLoop ]._sName;
                        IDBox.Text = _CarData[ iLoop ]._sIdentifier;
                    }
                }
            }
            else
                return;
        }


Any help is appreciated :)

Wazzak
Last edited on
Framework: Just post @ MSDN forums (http://social.msdn.microsoft.com/Forums). As for your problem: Use SelectedItems instead of SelectedIndices.
closed account (zb0S216C)
Thanks for the reply, WebJose. However, I replaced SelectedIndices with SelectedItems as you said, and it gives me this error:

Cannot implicitly convert type 'System.Windows.Forms.ListView.SelectedListViewItemCollection' to 'System.Windows.Forms.ListView.SelectedIndexCollection'


Wazzak
What I meant: Instead of using SelectedIndices and looping through the collection of Items (ListView.Items), the property SelectedItems automatically gives you the Items so you don't need to look them up by index.
Last edited on
closed account (zb0S216C)
OK. I've update the code above. So how do I get the index of the currently selected item?

P.S: I've set 'MultiCelect' to false

Wazzak
closed account (zb0S216C)
Thank you for you help, WebJose; I got it working. I'll update the code for other users :)

Wazzak
Topic archived. No new replies allowed.