Note that there are some explanatory texts on larger screens.

plurals
  1. POListview selectedItem
    text
    copied!<p>How do I use the <code>ListView</code>'s <code>ItemSelectionChanged</code> event? I have a <code>ListView</code>, which in turn has 8 columns; and the first column of each row contains the Id for the row and this is what I'm after.</p> <p>I need this because the Id is to be used to select info from a list. This list is what populates the <code>ListView</code>, but it doesn't show all of the info. The info not being shown is to be displayed in a <code>RichTextBox</code> and, of course, it should display the info for that selected item in the <code>ListView</code>. The Id's are the same, of course, but no matter what I try I can't seem to reach that value.</p> <p>My code so far:</p> <pre><code>foreach ([ClassName] x in [List]) { if (x.id == Convert.ToInt32(myListView.FocusedItem.Text)) { rtbxSpecific.Text = x.name; } } </code></pre> <p>This works great for the first object in the <code>ListView</code>, but not for any of the following where it throws a <a href="http://msdn.microsoft.com/en-us/library/system.nullreferenceexception.aspx" rel="nofollow"><code>NullReferenceException</code></a>. Why does this happen? It's like the event fires, but it doesn't know about any other item in the list except for the first one.</p> <p>I've also tried using <code>selectedItems[0].Text</code>, <code>selectedItems[0].SubItems[0].Text</code> but it's basically the same fault. This is really annoying me and I would be really thankful if someone could explain why it works for the first <code>ListViewItem</code>, but not the following.</p> <p>I even tried using <code>Find(...)</code> on the list with:</p> <pre><code>[Class] cl = [List].Find(delegate([Class] q) {return q.id == Convert.ToInt32(myListView.FocusedItem.SubItems[0].Text);}); </code></pre> <p>But no dice, which leads me to believe its the <code>FocusedItem</code> that is being nasty right now and I'm not getting anywhere with this. The <code>Find(...)</code> method would be cleaner to use, I suppose, but right now I'm getting the same error with both.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload