944,216 Members | Top Members by Rank

Ad:
May 17th, 2006
0

Fill a combobox

Expand Post »
Hi I am trying to fill a combobox with the files from a FileListBox the code i am using is

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2.  
  3. File1.Path "C:\Images"
  4. File1.Refresh
  5.  
  6. If File1.ListCount > 0 Then
  7.  
  8. For i = File1.ListCount To -1
  9. Combo1.AddItem File1.List(i)
  10. Next
  11. End If
  12.  
  13. End Sub
The FileListBox fills with the file from the folder that i want but the Combo Box doesn't come somebody give me some help with this please I think that the problem is with this line For i = File1.ListCount To -1

Alan
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
watkins6878 is offline Offline
5 posts
since Feb 2006
May 17th, 2006
0

Re: Fill a combobox

Try changing your for loop from a reverse count to a normal count:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. File1.Path = "C:\Images"
  3. File1.Refresh
  4.  
  5. If File1.ListCount > 0 Then
  6.  
  7. For i = 0 To File1.ListCount - 1
  8. Combo1.AddItem File1.List(i)
  9. Next
  10. End If
  11. End Sub
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 17th, 2006
0

Re: Fill a combobox

HI Comatose

Thanks that worked I am new to VB but am learning everyday can you explain to me why reversing it worked or even why it didnt work in the first place

Thanks
Reputation Points: 10
Solved Threads: 1
Newbie Poster
watkins6878 is offline Offline
5 posts
since Feb 2006
May 17th, 2006
0

Re: Fill a combobox

Technically, you could have "gone the other way", but when doing a for loop, it's default behavior is to count up. So, behind the scenes there is a test condition being applied to the for loop.... for example: for I = 0 to 9 is going to say "ok, is I less than 9" if the answer is yes, then do the stuff between for and next. Then, it's going to Add 1 to I, and say "ok, I is 1 [then 2 and 3, etc, etc], is it less than 9?" It will continue this process until I becomes 9. When you say: for I = 10 to 1, the programming language isn't going to change it's behavior, just because you decided to change the numbers.... so, dutifully, it says: "ok, I is 10 right off the bat, is I (10) less than 1 (or -1 or whatever)". Naturally, 10 is not less than 1 or negative 1.... or even 9, so the for loop never actually even runs once. It just compares, sees that 10 is not less than -1, and says screw it, and moves on.

You can change the behavior of the for loop, however, to decrement instead of increment (to count down instead of up) but be advised, that this is not the standard way of doing things. Meaning, if you ever share your code with someone, or you end up working in a team setting, someone might have a more difficult time understanding your code, because you aren't doing things normally. However, you could modify your statement to run backward with something like this:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. For i = File1.ListCount - 1 To 0 Step -1
  2. Combo1.AddItem File1.List(i)
  3. Next
The key here, is the "step" keyword. The step keyword tells the for loop what kind of behavior it should apply when dealing with the counter variable. You could do a for loop with a step 2, which would actually only execute code on every other iteration....
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 20th, 2006
0

Re: Fill a combobox

HI

thank you for the explanation i can see where you a going with this and i shall try to remember that when coding the next For loop .

Alan
Reputation Points: 10
Solved Threads: 1
Newbie Poster
watkins6878 is offline Offline
5 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: ps file
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Beep





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC