And the saga continues.....comboboxes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: And the saga continues.....comboboxes

  1. #1
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557

    And the saga continues.....comboboxes

    okie dokie....

    I have a grid <YAY!>. When the user double clicks on the grid, I want the textboxes/comboboxes to reflect the data in the grid. Here's what I have for the comboboxes:

    Code:
    
    cboJobNameHourly.ListIndex = -1
    If cboJobNameHourly.ListCount > 0 And .TextMatrix(HourlyRow, 0) <> "" Then
    For i = 0 To cboJobNameHourly.ListCount - 1
        If cboJobNameHourly.List(i) = .TextMatrix(HourlyRow, 0) Then Exit For
    Next i
    cboJobNameHourly.ListIndex = i
    End If
    
    Now........If the item is NOT in the combobox, I want to .additem. SO........where do I put that? Should I get rid of the Exit For and use a flag?

    L
    A balanced diet is a cookie in each hand.

  2. #2
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    the Exit for is ok, after the for loop just add (the . means your combo box)

    if i = .ListCount then <- not found
    .add ...
    .listindex = .listCount+1 <- select the new one
    else
    .listindex=i <- select what was found
    endif

    Marco

  3. #3
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557
    That would work, marco, but the combobox is sorting alphabetically so the new item might be in the middle of the list!

    Here's what I have. It's not as pretty as YOURS!

    Code:
    
    cboJobNameHourly.ListIndex = -1
    If cboJobNameHourly.ListCount > 0 And .TextMatrix(HourlyRow, 0) <> "" Then
    For i = 0 To cboJobNameHourly.ListCount - 1
        If cboJobNameHourly.List(i) = .TextMatrix(HourlyRow, 0) Then
        FoundIt = True
        Exit For
        End If
    Next i
    If FoundIt = True Then
    cboJobNameHourly.ListIndex = i
    Else
    cboJobNameHourly.AddItem .TextMatrix(HourlyRow, 0)
    'cboJobNameHourly.  THIS IS WHERE I'M STUCK!
    End If
    End If
    
    A balanced diet is a cookie in each hand.

  4. #4
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557
    AND.......

    I use this code about 25-30 times throughout this thingy. I guess I should create a function, huh?

    L
    A balanced diet is a cookie in each hand.

  5. #5
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Laurel,
    use the NewIndex property. It gives you the index of the newly added item
    Marco
    PS go for the function...

  6. #6
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557
    Here's what I did,marco. I didn't think you could use the text property (I thought it was read only) but evidentally you can use it to select the text IF the text is in the list.

    (Different combobox but same code)
    Code:
    
    If cboEmployeeNonHourly.ListCount > 0 And .TextMatrix(NonHourlyRow, 2) <> "" Then Call SelectComboItem(cboEmployeeNonHourly, .TextMatrix(NonHourlyRow, 2))
    
    Public Sub SelectComboItem(cbo As ComboBox, celltext As String)
    Dim i As Byte
     
    cbo.ListIndex = -1
    For i = 0 To cbo.ListCount - 1
        If cbo.List(i) = celltext Then Exit For
    Next i
    If i = cbo.ListCount Then cbo.AddItem celltext
    cbo.Text = celltext
     
    End Sub
    
    I used your code but in a function this time. If you can find a cleaner way, let me know!

    Love and kisses,
    Laurel
    A balanced diet is a cookie in each hand.

  7. #7
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Laurel,
    it looks fine.
    My only complain: do not use Byte, Long is much better. Internally, every number ends up being converted in a Long (that is the length of the word in modern CPU), thus it eliminates a lot of useless conversions. It is not worth it saving 3 bytes...
    BTW I use always and only Long (unless differently requested by a library), so I do not have to worry with annoying overflows (we think 16bits are enough until a customer call...)
    Otherwise, your fuction (sorry: method) is perfectly ok.
    Have a nice week end.
    Marco

  8. #8
    Join Date
    Jun 2004
    Location
    Houston area
    Posts
    557
    WOW! Thanks, marco!
    A balanced diet is a cookie in each hand.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center