Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Step -1 problems (2003) 4

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi all:

I'm trying to load a listbox with integers from 1 to 120. This works fine, but the lower #s are initiated when I hit the Up arrow. I need this to be reversed. So, going back to old VB6 days, I tried Step -1, but it does nothing.
Code:
Dim i As Int16
For i = 1 To 120 Step -1
 lbTask.Items.Add(i)
Next
It adds nothing to the listbox.

Why?

I've tried i = 120 to 1, but it adds nothing, either. I've even added the items to an arraylist and tried that way, still nothing.

Any ideas?


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
I figured it out...sort of:

Code:
Private Sub InitializeSeconds()
        
        Dim i As Int16
        For i = 1 To 120
            lbTask.Items.[b][COLOR=blue]Insert([i]0[/i][/color][/b], i)
            lbTests.Items.[b][COLOR=blue]Insert([i]0[/i][/color][/b], i)
            lbGlobal.Items.[b][COLOR=blue]Insert([i]0[/i][/color][/b], i)
            lbMSG.Items.[b][COLOR=blue]Insert([i]0[/i][/color][/b], i)

        Next
        
    End Sub

It still doesn't answer why?

I actually retried this in VB6 and got the same results as in .Net.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Have you tried
Code:
Dim i As Integer
For i = 1 To 120
   lbTask.Items.Add(i.ToString)
Next
?
 
this worked for me in 2008 and I don't see much reason why not in 2003

Code:
For i As Integer = 120 To 1 Step -1
  Me.ListBox1.Items.Add(i)
Next

tostring is not needed since vb.net does implicit conversion from integer to string. Just make sure that the sorted attribute of the listbox is set to false.

Christiaan Baes
Belgium

My Blog
 
Chrissie:

Don't ask me why this didn't work at home. The only difference was that I used Int16 and you used Integer. I'm beginning to think that the version I have at home is buggy, because this is not the first time something like this has happened.

I settled on a different way of doing it, similar to Mansii's way. I was, however, trying to reduce the rectangle portion of the listbox so that it came out only one index at a time. This was no problem. I eventually wound up using a spinner attached to the listbox and a label to hold the value.

I'll post that code later tonight when I get home. Definitely the long way of doing things.

Thanks for your help...as always.


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
The problem was in the bounds you gave your loop...

For i =1 to 120 step -1

vs

For i = 120 to 1 step -1


milan
 
MM:

Trust me, I tried that. I even put it into an arraylist to copy backwards to the listbox.

It was simply the version I have at home. I think I need a new SP.

I really went the long way around to solve it, but solved it is, and shall remain that way...Amen.

It looks very pretty, but very costly code wise. I promised to post it, and I shall when I have time from home.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
The Long Way Around:

I picked a spinner, a listbox, and a label to accomplish this (4 each, actually). Basically, when the user presses the up button, the LB Index subtracts 1. Viceversa for the down button.

Remember, the code above, even as Chrissie posted it, did not work on my PC

These values are for 4 different timers.

[Code>>Handlers]
Private Sub _SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles lbTask.SelectedIndexChanged, lbGlobal.SelectedIndexChanged, _
lbTests.SelectedIndexChanged, lbMSG.SelectedIndexChanged
''Label Text from Listbox Item
Select Case sender.Name
Case lbTask.Name
lTask.Text = lbTask.SelectedItem
Case lbGlobal.Name
lGlobal.Text = lbGlobal.SelectedItem
Case lbTests.Name
lTests.Text = lbTests.SelectedItem
Case lbMSG.Name
lMSG.Text = lbMSG.SelectedItem
End Select
End Sub '_SelectedIndexChanged

Private Sub _UpClick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles UDTask.UpClick, UDGlobal.UpClick, UDMsg.UpClick, UDTest.UpClick

''Spinner
Select Case sender.Name
Case UDTask.Name
Try
lbTask.SelectedIndex -= 1
Catch ex As Exception
lTask.Text = "120"
End Try
Case UDGlobal.Name
Try
lbGlobal.SelectedIndex -= 1
Catch ex As Exception
lGlobal.Text = "120"
End Try
Case UDMsg.Name
Try
lbMSG.SelectedIndex -= 1
Catch ex As Exception
lMSG.Text = "120"
End Try
Case UDTest.Name
Try
lbTests.SelectedIndex -= 1
Catch ex As Exception
lTests.Text = "120"
End Try
End Select

End Sub '_UpClick

Private Sub _DownClick(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles UDTask.DownClick, UDGlobal.DownClick, _
UDMsg.DownClick, UDTest.DownClick
''Spinner
Select Case sender.Name
Case UDTask.Name
Try
lbTask.SelectedIndex += 1
Catch ex As Exception
lTask.Text = "1"
End Try
Case UDGlobal.Name
Try
lbGlobal.SelectedIndex += 1
Catch ex As Exception
lGlobal.Text = "1"
End Try
Case UDMsg.Name
Try
lbMSG.SelectedIndex += 1
Catch ex As Exception
lMSG.Text = "1"
End Try
Case UDTest.Name
Try
lbTests.SelectedIndex += 1
Catch ex As Exception
lTests.Text = "1"
End Try
End Select

End Sub '_DownClick
[/code]

[code>>Load list boxes with integers]
Private Sub InitializeSeconds()

Dim i As Int16
For i = 1 To 120 lbTask.Items.Insert(0, i)
lbTests.Items.Insert(0, i)
lbGlobal.Items.Insert(0, i)
lbMSG.Items.Insert(0, i)
Next



End Sub
[/code]

It looks neat, and aesthetically pleasing, but definitely the long way around.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Erm, are you insane?

I've just put this in on VB2003...

Sub Main()
Dim s As String
Dim i As Integer

For i = 200 To 1 Step -1
Console.WriteLine(i.ToString())
Next
s = Console.Read()

End Sub

Worked perfectly...

Am I missing something?
 
Ron,
Have you tried uninstalling visual studio and reinstalling it? Sounds like something is corrupted.

I tried the following code in both VS 2003 and 2005. They both worked perfectly.

Dim i as Int16

For i = 120 to 1 Step -1
Listbox1.Items.Add(i)
Next


Sounds like you've got issues. Or at least your computer does.
 
PRPhx:

Alas, that is not what I wanted to do, but I did do it. Voila, it worked, so I guess it was worth it.

Thanks for the post.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top