-
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.
-
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
-
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.
-
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.
-
Laurel,
use the NewIndex property. It gives you the index of the newly added item
Marco
PS go for the function...
-
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.
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks