Subject: Listbox Conditional Iteration
Posted By: caterpillar Post Date: 9/1/2006 11:30:33 AM
Hi,

    I am iterating through a listbox control using for loop as:

Dim i1 as Int32
For i1 = listbox1.SelectedIndex + 1 To listbox1.Items.Count - 1
.............
.............
I need to know if somehow instead of reaching the end of list i should stop iterating upon finding a particular element say an element beginning with "T". I mean,  i need to replace "listbox1.Items.Count - 1" with some condition. But the problem here is that for loop runs with integer type values. I have tried do/while loops but they seem to be going into infinite loops.

Any guidance is welcome!

Regards
Monica

 



Reply By: gbianchi Reply Date: 9/1/2006 11:43:36 AM
hi there..

why cann't just do an exit for???

HTH

Gonzalo
Reply By: dparsons Reply Date: 9/1/2006 11:52:02 AM
For i1 = listbox1.SelectedIndex + 1 To listbox1.Items.Count - 1
 if [some condition] exit for
Next

To reiterate what gbianchi said. =]

"The one language all programmers understand is profanity."
Reply By: caterpillar Reply Date: 9/1/2006 3:22:21 PM
Guys, thanks for replying. Here is my code:
I have 2 listboxes list1 & list2:

Dim g As Int32 = 0
Dim h As Int32 = 0

Dim iCountI As Integer
iCountI = list2.Items.Count - 1

Dim iCountCl As Integer
iCountCl = list1.Items.Count - 1

Dim str1 As String = " "
str1 = list2.Items(iCountI - 1).Value.Substring(0)

If list1.SelectedIndex < iCountCl Then
    h = list1.SelectedIndex + 1
End If

If list2.SelectedIndex < iCountI Then
    g = list2.SelectedIndex + 1
End If

If list1.SelectedItem.Value.Substring(0, 1) <> "T" And list2.SelectedItem.Value.Substring(0, 1) <> "C" And list1.Items(h).Value.Substring(0, 1) <> "C" And list2.Items(g).Value.Substring(0, 1) <> "T" Then

Dim c1 As Int32 = 0
Dim i1 As Int32 = 0
Dim var1 As String = list1.SelectedItem.Value.Substring(1)
Dim var2 As String = list2.SelectedItem.Value.Substring(1)
Dim tvar1 As String = list2.Items(g).Value.Substring(0, 1)
                    
If list1.SelectedIndex <> -1 And lbItems.SelectedIndex <> -1 And list1.SelectedIndex <= list1.Items.Count - 1 And list2.SelectedIndex <= list2.Items.Count - 1
Then
    For c1 = list1.SelectedIndex + 1 To list1.Items.Count - 1
      For i1 = list2.SelectedIndex + 1 To list2.Items.Count - 1
        If c1 > 0 And c1 <= list1.Items.Count - 1 And i1 > 0 And i1 <= list2.Items.Count - 1 Then
          If list1.Items(c1).Value.Substring(1) = list2.Items(i1).Value.Substring(1) Then
     somefunction()
    Exit Sub
             End If
         End If
    Next i1
    Next c1
  End If
 End If
End If
''''var1,var2 & tvar1 are used for debugging purpose

I need to iterate from the two lists. The lists are arranged as:

Day-LIST1                                          Topic-LIST2
---------                                          ----------   
Day1                                                topic1
 -topic1                                             -Day1
 -topic3                                             -Day4   
Day2                                                topic2    
Day3                                                 -Day3
 -topic3                                            topic3   
 -topic2                                             -Day1   
Day4                                                 -Day3
 -topic1

I am attaching & dettaching days(LIST1) with topic(LIST2) & vice versa using the above code. Say if i select Day3 from List1 & topic2 from List2 and call somefunction() to de-attach, it removes topic2 from List1 & Day3 from List2 only against the selected value-set and not elsewhere. But again if i select Day3 from List1 but topic1 from List2, it is expected not to do anything  as there is no matching pair in the subset,but it is removing Day3 from List2. I need to stop this. Also as shown in the code,the Ist character of any DAY begins with 'C' and the Ist character of any TOPIC begins with 'T' . I have been using it above for few validations check.
The datatypes are varchar.
I hope its clear now. I need to validate the dettaching thing.

Reply By: caterpillar Reply Date: 9/1/2006 4:26:35 PM
In my last post list2 was wrongly printed as lbItems at 1 place.
Apologies...

Regards

Reply By: dparsons Reply Date: 9/1/2006 5:36:39 PM
dim li1, li2 as ListItem

for each li1 in list1.items
   for each li2 in list2.itmes
   next
next

Program logic is up to you but that will iterate through the listbox collections and then you can compare the values.

"The one language all programmers understand is profanity."

Go to topic 48595

Return to index page 186
Return to index page 185
Return to index page 184
Return to index page 183
Return to index page 182
Return to index page 181
Return to index page 180
Return to index page 179
Return to index page 178
Return to index page 177