Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Heya all... Could someone please assist me in sorting out my "slight" dilema? :confused:

 

I'm trying to loop through thru the values in one list box(lstSelect) and compare them to a second list box(lstSelected). If the value in lstSelected is a vlaue from lstSelect then a msgbox should popup with an error message. Pretty simple I know... but I'm starting to create nested try catch clauses, and it is sarting to get a little hairy for me.

 

Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click

       Dim os As ListBox.SelectedObjectCollection 'declare the collection object

       Try
           lstSelected.Items.Add(lstSelect.Text)
           lstSelect.Items.Remove(lstSelect.Text)
           Try
               os = lstSelected.SelectedItems 'fill it in
               For x As Integer = 0 To os.Count - 1 'go through each and display it
                   MessageBox.Show(Convert.ToString(os.Item(x)))

                   If lstSelect.Text = x Then
                       MsgBox("Cannot Process", MsgBoxStyle.OKOnly, "Erg!")
                   End If
               Next

           Catch ex As Exception
               MsgBox(Err.Number + " " + Err.Description, MsgBoxStyle.OKOnly, "Error")

           End Try

       Catch ex As Exception
           MsgBox("Error Type : " & Err.Description, MsgBoxStyle.Exclamation, "Error Number : " & Err.Number)
       End Try

   End Sub

 

Attached is the code. If someone can find the problem or direct me in the right direction I would be greatly appreciative...

 

Ta in advance.

P.L.U.R - Peace, Love, Unity, Respect :P
  • *Experts*
Posted

If all you are trying to do is see if items match on two listboxes then that is too much code :).

Here is how you can do it:

(Im assuming that both Listboxes have the same amount of items, if not correct me)

For x As Integer = 0 To lstSelected.Items.Count - 1
    If lstSelected.Items.Item(x) = lstSelect.Items.Item(x) Then
        MessageBox.Show("Cannot process")
    End If
Next

Posted
Function FoundMatch() As Boolean
       FoundMatch = False
       'The above example will not work unless both boxes have the same data in the same order
       'This routine will kick out a match no matter what data is in either box
       Dim j, i As Integer
       For j = 0 To lstSelected.Items.Count - 1
           For i = 0 To lstSelect.Items.count - 1
               If lstSelected.Items.Item(j) = lstSelect.Items.Item(i) Then
                   Return True
               End If
           Next i
       Next j
End Function

Posted

difference in values

 

Thanx for the help Mutant it is the closest I have come to figuring it out... Unfortuntaely the values in the list boxes, listbox1 is always a constant order of vlaues based on a SQL Database table field. listbox2's values are selected by the user using a button and can select the values in any order - So I'm trying to find a way of looping through listbox2 to see if the new items to add from listbox1 are present or not. If present then Error msg.

 

Thanx a million again

P.L.U.R - Peace, Love, Unity, Respect :P
Posted

solution evading

 

The code that FartKnocker gave is useful, but no matter how I alter and manipulate it, it does not work.

 

Problem is such : I have a button on my form, when it is clicked a value from listbox1 is send to listbox2. What I'm trying to do it is stop a new value being entered into listbox2 if it is already present in listbox2.

P.L.U.R - Peace, Love, Unity, Respect :P
  • Leaders
Posted (edited)

i recently helped someone on another forum , with how to move selected items from one listbox to another using an arraylist ( no looping ) . based on this i've knocked this together ( although you must loop through the arraylist to compare the items in listbox2. hope this helps...

       Dim lbCol As New ListBox.SelectedObjectCollection(ListBox1)
       Dim strItem As String
       For Each strItem In ArrayList.Adapter(lbCol).ToArray
           If ListBox2.Items.IndexOf(strItem) > -1 Then '/// if it's also in listbox2.
               MessageBox.Show(ListBox2.Items(ListBox2.Items.IndexOf(strItem)))
               ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf(strItem))
               '/// you can remove the selected items from listbox2 using the indexof ListBox2.SelectedItems.IndexOf(strItem)
           End If
       Next
       '/// now we can add the selected items.
       ListBox2.Items.AddRange(ArrayList.Adapter(lbCol).ToArray)

Edited by dynamic_sysop

Posted

Your problem is probably just a case issue. When comparing items, it's best to convert both to one case or the other or the results will never be what you'd expect. The following simple code converts both to upper case and therefore, must work...

 

Function FoundMatch() As Boolean
       FoundMatch = False
       'The above example will not work unless both boxes have the same data in the same order
       'This routine will kick out a match no matter what data is in either box
       Dim j, i As Integer
       For j = 0 To lstSelected.Items.Count - 1
           For i = 0 To lstSelect.Items.count - 1
               If UCase(lstSelected.Items.Item(j)) = UCase(lstSelect.Items.Item(i)) Then
                   Return True
               End If
           Next i
       Next j
End Function

Posted (edited)

Eureka!!!

 

Right I got it sorted out(all it took was 2 shots of JD - who would have thought:confused: )

 

It was not a case issue as fartknocker pointed out. Thanx for the code you submitted though it did help me isolate where my problem was. And from my own error it did not have to do with the listbox.text method either... I used the listbox.selectedindex method - I thought it would be easier since I was using fartknockers recomendation of the for loop. :p stupid me...

 

here follows the code

 

Function FoundMatch() As Boolean

       FoundMatch = False

       Dim i As Integer

       For i = 0 To lstSelected.Items.Count - 1
           If lstSelect.SelectedIndex <> i Then
               FoundMatch = False
           End If

           If i = lstSelect.SelectedIndex Then
               FoundMatch = True
           End If
       Next i

   End Function

 

to use it just put a call in your sub to the FoundMatch() and it will do the rest.

 

Thanx for everyones help...

 

Regards

Edited by absent
P.L.U.R - Peace, Love, Unity, Respect :P

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...