Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Please take a look at this For…Next code and tell me what's wrong with it because the items doesn't want to show in the ComboBox at Runtime.
VB
Dim verses As Integer

        For verses = 1 To 50 Step 1
            chaptersComboBox.Items.Add(verses.ToString)
        Next
Posted
Updated 31-Aug-11 22:09pm
v2

There is nothing wrong with the code you have shown, it will list the numbers 1 to 50 in the combobox dropdown.

It could also be written like this;
VB
For verses as Integer = 1 to 50
    chaptersComboBox.Items.Add(verses.toString)
Next


The variable is declared as part of the for statement, the default step on a x to Y operation is 1, so doesn't need to be specificed.

Maybe there is something else executing that is clearing the combobox items collection after this code has executed?
 
Share this answer
 
I would use foreach instead.
This way you don't need to worry about a count.

VB
For Each item As string chaptersComboBox.Items
    result = item
Next
 
Share this answer
 
v2
Comments
Prerak Patel 1-Sep-11 5:30am    
Not voting for the answer, but I think OP is trying to add items, not reading them.
Abhinav S 1-Sep-11 6:30am    
Ah ok. In that case, the OP has not provided enough information to help him.
Maybe he needs to check if he has an incorrect display member set in the combo box.
I just tried it and it works fine.
An empty combobox, press the button, it fills with the numbers 1 to 50 as expected.


The only thing which didn't happen was that nothing appeared in the drop down part until I entered the control and pressed the drop arrow. Easily solved with the chaptersComboBox.SelectedIndex property.
 
Share this answer
 

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