Click here to Skip to main content
15,912,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
2 Textboxs
2 list boxs
do while loop

I want to change the direction of the result in the Listbox to change based on the input of the textbox. So if Txtbox 1 = 0 Txtbox 2 = 10 then Listbox 1 = 1,3,5,7,9 Listbox 2 - 2,4,6,8,10 and if Txtbox 1 = 10 Txtbox 2 = 0 then Listbox 1 = 9,7,5,3,1 Listbox 2 = 10,8,6,4,2

What i am trying to do
VB
LowerB = CType(txtLB.Text, Integer)
            UpperB = CType(txtUB.Text, Integer)

            If LowerB < UpperB Then
                NumCounterI = CountUp
            Else
                NumCounterI = Countdown
            End If

            If LowerB < UpperB Then
                NumCounterI = LowerB

                Do While NumCounterI <= UpperB     
                    If NumCounterI <> 0 Then
                        If NumCounterI Mod 2 = EvenNumber Then
                            lstEvens.Items.Add(NumCounterI)
                        Else
                            lstOdds.Items.Add(NumCounterI)
                        End If
                    End If
                    NumCounterI += 1
                Loop
Posted
Updated 16-May-12 21:39pm
v2
Comments
VJ Reddy 17-May-12 22:52pm    
Please see Solution 2. I have added an alternative with Do While loop
Thank you.
VJ Reddy 20-May-12 0:22am    
Thank you for accepting the solution :)

A Simple Solution:
VB
If LowerB < UpperB Then
    IncrementStep = 1
Else
    IncrementStep = -1
End If

For NumCounterI As Integer = LowerB To UpperB Step IncrementStep
        If NumCounterI Mod 2 = 0 Then
             ListBox1.Items.Add(NumCounterI.ToString())
        Else
               ListBox2.Items.Add(NumCounterI.ToString())
        End If
Next


rgds
 
Share this answer
 
v2
Comments
Maciej Los 17-May-12 13:03pm    
Good example, 5!
Sergey Alexandrovich Kryukov 18-May-12 14:07pm    
Good enough and simple, a 5.
--SA
I think LINQ can be used as follows to generate the sequences
VB
Dim lb As Integer = 10
Dim ub As Integer = 0
Dim seq = Enumerable.Range(Math.Min(lb, ub), Math.Max(lb, ub) / 2)
Dim seq1 = If(lb > ub, seq.[Select](Function(s) s * 2 + 1).Reverse(), _
                    seq.[Select](Function(s) s * 2 + 1))
Dim seq2 = If(lb > ub, seq.[Select](Function(s) s * 2 + 2).Reverse(), _
                    seq.[Select](Function(s) s * 2 + 2))


[Edit]Alternative using do while loop added as per the comment of OP [/Edit]
VB
Dim lb = 10
Dim ub = 0
Dim list1 as new List(Of integer)
Dim list2 as new List(Of integer)
Dim inc = IF(lb < ub, 2, -2)
Dim seed1 = IF(lb < ub, 1, 9)
Dim seed2 = IF(lb < ub, 2, 10)
Dim ctr = 0
do while ctr < 5
    list1.Add(seed1 + ctr * inc)
    list2.Add(seed2 + ctr * inc)
    ctr += 1
loop
 
Share this answer
 
v2
Comments
Maciej Los 17-May-12 13:02pm    
Good answer, my 5!
VJ Reddy 17-May-12 13:44pm    
Thank you, losmac :)
Sergey Alexandrovich Kryukov 18-May-12 14:07pm    
Using LINQ for such things is a good idea, my 5.
--SA
VJ Reddy 18-May-12 20:25pm    
Thank you, SA :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900