Equals

Jedhi

Well-known member
Joined
Oct 2, 2003
Messages
127
Following code does always fails even when they contain the same. Reason is that they are of two different type(string and ListViewItem). How do I fix it ?

// nostr is a string
foreach (ListViewItem item in listView1.Items)
{
if (item(nostr))
item.SubItems[4].Text = "Equal";
}
 
id do something along these lines ...
C#:
				foreach(ListViewItem lvi in listView1.Items)
				{
					foreach(ListViewItem.ListViewSubItem lvs in lvi.SubItems)
					{
						if(lvs.Text=="equals")
						{
							Console.WriteLine(lvs.Text + " was found at index:" + lvi.SubItems.IndexOf(lvs).ToString());
						}
					}
				}
 
Is it really necessary to run through all list items and sub items to see if a subitem is equal a textstring ?

What is giving the problem is when I try to compare an item from Listview with a string. !!!

if (item(nostr)) should have been replaced with
if (item.Equals(nostr)). Sorry.
 
without looping the subitems seperatly , you can do this ...
C#:
				foreach(ListViewItem lvi in listView1.Items)
				{
					if(lvi.SubItems.Count > 4) /// make sure we have enough subitems
					{
						ListViewItem.ListViewSubItem lvs = (ListViewItem.ListViewSubItem)lvi.SubItems[4];
						if(lvs.Text == "equals")
						{
							MessageBox.Show(lvs.Text);
						}
					}
				}
 
Back
Top