## Visual Basic

Moderators: None (Apply to moderate this forum)
Number of posts: 55384

This Forum Only

Help Me With This Please Posted by sad.boy on 13 Feb 2006 at 10:43 AM
look at this code,it removes any odd numbers in combobox1 when command 1 is clicked
Private Sub Command1_Click()
i = 0
While i <= Combo1.ListCount - 1
Combo1.ListIndex = i
If Combo1.List(Combo1.ListIndex) Mod 2 = 1 Then
Combo1.RemoveItem Combo1.ListIndex
i = -1
Print i
End If
i = i + 1
Wend
End Sub

I'm Wondering in the while condition why we have to say Combo.listcount -1 and combo1.listcount would be wrong without -1
why is that?:-?
another thing is that why we should change the value of i to -1 when
when the conditioin is true?
No One Knows What Is Like To Be The Sad Boy
Re: Help Me With This Please Posted by DrMarten on 13 Feb 2006 at 8:03 PM
: look at this code,it removes any odd numbers in combobox1 when command 1 is clicked
:
: Private Sub Command1_Click()
: i = 0
: While i <= Combo1.ListCount - 1
:  Combo1.ListIndex = i
:  If Combo1.List(Combo1.ListIndex) Mod 2 = 1 Then
:    Combo1.RemoveItem Combo1.ListIndex
:    i = -1
:   Print i
:   End If
: i = i + 1
: Wend
: End Sub
:

: I'm Wondering in the while condition why we have to say Combo.listcount -1 and combo1.listcount would be wrong without -1
: why is that?:-?
: another thing is that why we should change the value of i to -1 when
: when the conditioin is true?
: No One Knows What Is Like To Be The Sad Boy
:
=============================================
Firstly why the name "sadBoy" and the comment>>
"No One Knows What Is Like To Be The Sad Boy"?

Anyway the -1 is probably to do with the fact that VB starts counting at zero except for when it gets a string length or in this case a combobox listcount.

The first item then is item number 0 ( not number 1 ).

I will pass on the 2nd question as i can not see the reason for it,but won't your print statement always print -1?

Regards,

Dr M.

Re: Help Me With This Please Posted by Mike_AB1 on 14 Feb 2006 at 6:24 AM
: : look at this code,it removes any odd numbers in combobox1 when command 1 is clicked
: :
: : Private Sub Command1_Click()
: : i = 0
: : While i <= Combo1.ListCount - 1
: :  Combo1.ListIndex = i
: :  If Combo1.List(Combo1.ListIndex) Mod 2 = 1 Then
: :    Combo1.RemoveItem Combo1.ListIndex
: :    i = -1
: :   Print i
: :   End If
: : i = i + 1
: : Wend
: : End Sub
: :

: : I'm Wondering in the while condition why we have to say Combo.listcount -1 and combo1.listcount would be wrong without -1
: : why is that?:-?
: : another thing is that why we should change the value of i to -1 when
: : when the conditioin is true?
: : No One Knows What Is Like To Be The Sad Boy
: :
: =============================================
: Firstly why the name "sadBoy" and the comment>>
: "No One Knows What Is Like To Be The Sad Boy"?
:
: Anyway the -1 is probably to do with the fact that VB starts counting at zero except for when it gets a string length or in this case a combobox listcount.
:
: The first item then is item number 0 ( not number 1 ).
:
: I will pass on the 2nd question as i can not see the reason for it,but won't your print statement always print -1?
:
:
: Regards,
:
: Dr M.
:
:
:

For the second question:
Say you have 3 items in the combo box
You set listIndex to 1, do your validation and then remove this item.
now former second item becomes first one - so you have to do validation for list index 1 again.
Actually the line
i = -1
Should be
i =i - 1 (to compensate for later i = i + 1)

Mike

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic