Results 1 to 8 of 8

Thread: Help me!!!!!!!!!

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55

    Help me!!!!!!!!!

    I have a ComboBox control, I get the data from database and add it to the ComboBox like this:
    VB Code:
    1. With cboEmployee
    2.     Do Until rs.EOF
    3.         .AddItem rs.Fields("EmployeeName")
    4.         .ItemData(.NewIndex) = rs.Fields("EmployeeID")
    5.         rs.MoveNext
    6.     Loop
    7. 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:
    1. For intCount = 0 To cboEmployee.ListCount - 1
    2.     If cboComboBox.ItemData(intCount) = intItemData Then
    3.         cboComboBox.ListIndex = intCount
    4.         Exit For
    5.     End if
    6. Next intCount

    Who can help me improve the code, thanks in advance!!!

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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:
    1. With cboEmployee
    2.     Do Until rs.EOF
    3.         .AddItem rs.Fields("EmployeeName")
    4.         .ItemData(.NewIndex) = rs.Fields("EmployeeID")
    5.         rs.MoveNext
    6.     Loop
    7. 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:
    1. For intCount = 0 To cboEmployee.ListCount - 1
    2.     If cboComboBox.ItemData(intCount) = intItemData Then
    3.         cboComboBox.ListIndex = intCount
    4.         Exit For
    5.     End if
    6. 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?

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    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.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Originally posted by Shaggy Hiker
    Mendhak is having a bad frog day.

    I hope so

  5. #5
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    Originally posted by Shaggy Hiker
    Does it offend your sensibilities?[/B]

    Larf


    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    That's legit. Sometimes code just doesn't feel right. That's when I start looking around for a different way to write it.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    55
    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!

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    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
  •  



Click Here to Expand Forum to Full Width