Results 1 to 2 of 2

Thread: Populating listbox with system colors

Hybrid View

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Posts
    36

    Question Populating listbox with system colors

    I searched the messages in this forum for a solution to this problem, and found several posts regarding how to populate a listbox control with the system colors. One of the more concise solutions I found went something like this:


    Dim Colors() As String = KnownColor.GetNames_
    (GetType(KnownColor))
    Dim ColorEnum As String

    For Each ColorEnum In Colors
    If Not Color.FromName(ColorEnum).IsSystemColor Then
    ListBox1.Items.Add(Color.FromName(ColorEnum))
    End If
    Next

    So, with that portion of code in mind, would it be "wrong" to do this instead:

    Dim Colors() As String = KnownColor.GetNames_
    (GetType(KnownColor))
    Dim ColorCounter As Integer

    For ColorCounter = 0 To Colors.GetUpperBound(0)
    If Not Color.FromName_
    (Colors(ColorCounter)).IsSystemColor Then
    ListBox1.Items.Add(Colors(ColorCounter))
    End If
    Next ColorCounter

    basically, using a FOR-NEXT loop in place of the FOR-EACH loop? I don't know if this type of thing is frowned upon in VB.NET.

    Just curious - any input would be appreciated!

    --Steve K.
    [email protected]

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No I don't think it is frowned on. I also don't see any pros to doing it this way. What is the purpose in NOT using For-Each?

    In VB6 you could get better speed by doing it similiar to that way only you neget the speed by still requesting the GetUpperBound in the For statement. Also I'm not sure if the speed of a For-Each is as much of an issue in .NET.

    In VB6 this would be faster than a For-Each:
    VB Code:
    1. Dim Colors() As String = KnownColor.GetNames_
    2. (GetType(KnownColor))
    3. Dim ColorCounter As Integer
    4. Dim ColorMax As Integer=Colors.GetUpperBound(0)
    5.  
    6. For ColorCounter = 0 To ColorMax 'having a static number stops it from checking this number at the start of every loop
    7. If Not Color.FromName_
    8. (Colors(ColorCounter)).IsSystemColor Then
    9. ListBox1.Items.Add(Colors(ColorCounter))
    10. End If
    11. Next 'I believe it is actually faster when you don't specify the Next variable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width