Results 1 to 6 of 6

Thread: ComboBox toughie

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    ComboBox toughie

    I've researched this one and concluded it can't be done, but I'll see if anyone here has any ideas.

    On a form for a record, I have a combobox of "port names" which are read from a file. When the user views the record, the record's port name will be displayed in the combobox, but the program will hold off reading the file for all the ports until the user clicks on the combobox.

    The purpose of this is so the program starts up quickly, without unnecessary file reads.

    So the challenge is to make the combobox behave *as if* it were already loaded with the ports, so when the user clicks, it drops down and the port that was displayed is shown as selected in the dropped down list.

    I could only get as far as filling the ports, and using SendMessage to drop down the list. I could not get the program to keep the list dropped down with that item selected.
    end war
    stop greed

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Like this?
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     ' Do whatever you do to set the intial
    5.     ' value of the Port here from your DataSource
    6.     ' And make sure it's selected.
    7.     cboPort.AddItem "21"
    8.     cboPort.ListIndex = 0
    9. End Sub
    10.  
    11. Private Sub cboPort_DropDown()
    12.     Static bPortsLoaded As Boolean
    13.     ' If the Ports haven't been loaded yet
    14.     ' Load them Now.
    15.     If Not bPortsLoaded Then LoadPorts
    16.    
    17.     ' Only Load the Ports once, so track
    18.     ' That we've done it in a Static variable.
    19.     bPortsLoaded = True
    20. End Sub
    21.  
    22. Private Sub LoadPorts()
    23.     Dim iFile As Integer
    24.     Dim sPorts As String
    25.     Dim vPort As Variant
    26.     Dim lPort As Long, lIndex As Long
    27.    
    28.     ' Open the Port File
    29.     iFile = FreeFile
    30.     Open "C:\Ports.txt" For Binary Access Read As iFile
    31.         sPorts = Space(LOF(iFile))
    32.         Get #iFile, , sPorts
    33.     Close iFile
    34.     ' Split it into Port Entries
    35.     vPort = Split(sPorts, vbCrLf)
    36.    
    37.     ' Remember the current Port
    38.     sPorts = cboPort.Text
    39.    
    40.     ' Clear the Combo
    41.     cboPort.Clear
    42.    
    43.     ' Populate the Combo with ALL the ports.
    44.     For lPort = 0 To UBound(vPort)
    45.         cboPort.AddItem vPort(lPort)
    46.         ' If this is the port that was selected
    47.         ' Remember the ListIndex, so it's selected in
    48.         ' The DropList.
    49.         If vPort(lPort) = sPorts Then lIndex = lPort
    50.     Next
    51.     ' Set the ListIndex to the Original Port
    52.     ' So it's Selected when the list is Dropped.
    53.     cboPort.ListIndex = lIndex
    54. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497

    Thumbs up

    Oh my g*d. Thank you. That was so painfully easy.

    My mistake, and the part I left out, was that my combo box had a style=2=dropdown combo. This because I don't really want the user typing anything in there. But I can code around that--this was the part I really wanted to get.

    Shanti Oum and thank you!!
    end war
    stop greed

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497
    Mr. Young! I applied the method you suggested to my real application, using real data and it does not work. The reason, I think, is because of the time taken between the DropDown event start and the finish of the load and listindex selection of the combobox.

    To read the ports into the combobox, my app has to execute a remote command which calls at least one sql statement, then opens a text file to line input and do some string manip of each line. All this, I think, takes too long and the dropdown goes away.

    Is there any way you know of around this? Is there a way to give windows a longer delay before it cancels things like the dropdown?

    I knew it was hard. The combobox style thing was not my problem.
    end war
    stop greed

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    NY
    Posts
    497
    If anyone cares, I solved this problem thusly:

    Place a timer on the form to handle this. In its Timer event, place the API SendMessage call to dropdown the cmbPort combobox. But wait...there's more...

    Place a combobox on the form cmbPortStart and add one item=the port of the record being displayed, set the ListIndex=0.

    Place the combobox that will hold all the ports avail, cmbPort, beneath cmbPortStart and set its Visible property=False.

    In cmbPortStart_DropDown, call code to fill cmbPort and set its ListIndex to the item in cmbPortStart.Text (as Aaron described with just a single combobox in the mix).

    Set cmbStartPort's Visible property to False, and cmbPort's Visible property to true, and THEN enable that timer.

    So far, it's working and both comboboxes have a style=2.
    end war
    stop greed

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Thumbs up

    Thank you for posting that vbMom.

    As I've said many times, a solution posted will help those that, as yet, do not have the problem.

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