bjwade62 Posted January 26, 2006 Posted January 26, 2006 I have two listviews. lvwA and lvwB. I'd like to search lvwB for the existence of the same item in lvwA. I'm not sure how to go about it. Any help? Thanks. Quote
Cags Posted January 27, 2006 Posted January 27, 2006 Initially I was going to say you could use the Contains method of the Items collection however upon a quick test that didn't seem to work. One way of doing it would be something like this... private void cmdSearch_Click(object sender, System.EventArgs e) { if(lvwA.SelectedItems.Count!=0) { string searchText = lvwA.SelectedItems[0].Text; bool found = FindItem(searchText); } } private bool FindItem(string searchText) { for(int i = 0; i < lvwB.Items.Count; i++) { if(lvwB.Items[i].Text == searchText) { return true; } } return false; } Quote Anybody looking for a graduate programmer (Midlands, England)?
bjwade62 Posted January 27, 2006 Author Posted January 27, 2006 Sorry. I should have been more specific. I'm using VB in VS2005. Initially I was going to say you could use the Contains method of the Items collection however upon a quick test that didn't seem to work. One way of doing it would be something like this... private void cmdSearch_Click(object sender, System.EventArgs e) { if(lvwA.SelectedItems.Count!=0) { string searchText = lvwA.SelectedItems[0].Text; bool found = FindItem(searchText); } } private bool FindItem(string searchText) { for(int i = 0; i < lvwB.Items.Count; i++) { if(lvwB.Items[i].Text == searchText) { return true; } } return false; } Quote
Cags Posted January 27, 2006 Posted January 27, 2006 Private Function FindItems(ByVal searchText As String) As Boolean For i As Single = 0 To i < lvwB.Items.Count If (lvwB.Items(i).Text = searchText) Then Return True End If Next Return False End Function Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (lvwA.SelectedItems.Count <> 0) Then Dim searchText As String = lvwA.SelectedItems(0).Text Dim found As Boolean = FindItems(searchText) End If End Sub Quote Anybody looking for a graduate programmer (Midlands, England)?
bjwade62 Posted January 27, 2006 Author Posted January 27, 2006 Wow. Excellent. Thank you so much. Works great. Now I'll have to break it down to learn from what you did. Thanks again! Private Function FindItems(ByVal searchText As String) As Boolean For i As Single = 0 To i < lvwB.Items.Count If (lvwB.Items(i).Text = searchText) Then Return True End If Next Return False End Function Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If (lvwA.SelectedItems.Count <> 0) Then Dim searchText As String = lvwA.SelectedItems(0).Text Dim found As Boolean = FindItems(searchText) End If End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.