简体   繁体   中英

listview selected item error

I have a listview in another thread, i add items to it in safe thread-safe manner, like this:

listView1.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString);

but when i tried to get a selected items, it say the index 0 is invalid.

i used this:

string IpAdr = listView1.SelectedItems[0].SubItems[0].Text;

error = "InvalidArgument=Value of '0' is not valid for 'index'.\\r\\nParameter name: index"

then since it is on another thread, i tried to invoke like this:

 public string GetCurrentItem(int location)
    {
        if (this.listView1.InvokeRequired)
        {
            getCurrentItemCallBack d = new getCurrentItemCallBack(GetCurrentItem);
            return this.Invoke(d, new object[] { location }).ToString();
        }
        else
        {
            return this.listView1.Items[location].Text;
        }
    }

when i call, i got the same error.

i can't understand what is wrong.

any help is appreciated. thx.

Try to use ListView.SelectedIndices Property

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.selectedindices.aspx

if (this.listView1.SelectedIndices.Count > 0) 
{ 
     string IpAdress = listView1.Items[listView1.SelectedIndices[0]].Text; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM