Fill a combobox

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2006
Posts: 5
Reputation: watkins6878 is an unknown quantity at this point 
Solved Threads: 1
watkins6878 watkins6878 is offline Offline
Newbie Poster

Fill a combobox

 
0
  #1
May 17th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Fill a combobox

 
0
  #2
May 17th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 5
Reputation: watkins6878 is an unknown quantity at this point 
Solved Threads: 1
watkins6878 watkins6878 is offline Offline
Newbie Poster

Re: Fill a combobox

 
0
  #3
May 17th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Fill a combobox

 
0
  #4
May 17th, 2006
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....
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 5
Reputation: watkins6878 is an unknown quantity at this point 
Solved Threads: 1
watkins6878 watkins6878 is offline Offline
Newbie Poster

Re: Fill a combobox

 
0
  #5
May 20th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC