|
-
Apr 11th, 2004, 09:11 AM
#1
Thread Starter
Member
Help me!!!!!!!!!
I have a ComboBox control, I get the data from database and add it to the ComboBox like this:
VB Code:
With cboEmployee
Do Until rs.EOF
.AddItem rs.Fields("EmployeeName")
.ItemData(.NewIndex) = rs.Fields("EmployeeID")
rs.MoveNext
Loop
End With
Now, I get one employee id from the database,I want to the cboEmployee display the name relating the id. I write the code like this(so foolish) 
VB Code:
For intCount = 0 To cboEmployee.ListCount - 1
If cboComboBox.ItemData(intCount) = intItemData Then
cboComboBox.ListIndex = intCount
Exit For
End if
Next intCount
Who can help me improve the code, thanks in advance!!!
-
Apr 11th, 2004, 11:08 AM
#2
Re: Help me!!!!!!!!!
Originally posted by xunxm
I have a ComboBox control, I get the data from database and add it to the ComboBox like this:
VB Code:
With cboEmployee
Do Until rs.EOF
.AddItem rs.Fields("EmployeeName")
.ItemData(.NewIndex) = rs.Fields("EmployeeID")
rs.MoveNext
Loop
End With
Now, I get one employee id from the database,I want to the cboEmployee display the name relating the id. I write the code like this(so foolish) 
VB Code:
For intCount = 0 To cboEmployee.ListCount - 1
If cboComboBox.ItemData(intCount) = intItemData Then
cboComboBox.ListIndex = intCount
Exit For
End if
Next intCount
Who can help me improve the code, thanks in advance!!!
I can't understand, what are you trying to accomplish with the second piece of code? Why are you assigning Indexes twice, or what is your aim here?
-
Apr 11th, 2004, 11:16 AM
#3
Mendhak is having a bad frog day.
You want to improve that code? In what way? Is it too slow? Does it offend your sensibilities? Does it work?
It seems pretty straightforward, you are manually filling a combobox, then setting the selected item. There may be some bound control solution that would save a step, but it wouldn't be better in my opinion. It might be simpler, and take less code, but I prefer to keep the control to myself.
-
Apr 11th, 2004, 11:18 AM
#4
Originally posted by Shaggy Hiker
Mendhak is having a bad frog day.
I hope so
-
Apr 11th, 2004, 11:25 AM
#5
Originally posted by Shaggy Hiker
Does it offend your sensibilities?[/B]
Larf 
Phreak
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Apr 11th, 2004, 11:37 AM
#6
That's legit. Sometimes code just doesn't feel right. That's when I start looking around for a different way to write it.
-
Apr 11th, 2004, 07:48 PM
#7
Thread Starter
Member
Sorry, My english is poor, so.......
For Example:
Employees(Table)
ID Employee
1 Mike
2 John
6 Bob
8 Sue
...............
I use The first code to add data to ComboBox. In ItemData, I add the ID value, the List I add the EmployeeName value. When I choose a Employee in the ComboBox, I can get his ID.
But, I get a Employee's ID by another produre, when I get the ID, I want to the ComboBox can show the EmployeeName, So I have to use the second code. But I think it's so foolish, so I want to have another way to improve it. thanks!
-
Apr 11th, 2004, 08:07 PM
#8
See, it offends your sensibilities.
There may be a different way to do this, but I'm not sure that there is a better way. If it works, and it is fast enough for you, you might as well leave it alone. On the other hand, if it doesn't behave quite the way you want it to, that's a good reason to fix it, but it sounds like it is working fine for you.
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
|
Click Here to Expand Forum to Full Width
|