|
-
Feb 22nd, 2002, 09:11 PM
#1
Thread Starter
Hyperactive Member
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.
-
Feb 22nd, 2002, 11:42 PM
#2
Like this?
VB Code:
Option Explicit
Private Sub Form_Load()
' Do whatever you do to set the intial
' value of the Port here from your DataSource
' And make sure it's selected.
cboPort.AddItem "21"
cboPort.ListIndex = 0
End Sub
Private Sub cboPort_DropDown()
Static bPortsLoaded As Boolean
' If the Ports haven't been loaded yet
' Load them Now.
If Not bPortsLoaded Then LoadPorts
' Only Load the Ports once, so track
' That we've done it in a Static variable.
bPortsLoaded = True
End Sub
Private Sub LoadPorts()
Dim iFile As Integer
Dim sPorts As String
Dim vPort As Variant
Dim lPort As Long, lIndex As Long
' Open the Port File
iFile = FreeFile
Open "C:\Ports.txt" For Binary Access Read As iFile
sPorts = Space(LOF(iFile))
Get #iFile, , sPorts
Close iFile
' Split it into Port Entries
vPort = Split(sPorts, vbCrLf)
' Remember the current Port
sPorts = cboPort.Text
' Clear the Combo
cboPort.Clear
' Populate the Combo with ALL the ports.
For lPort = 0 To UBound(vPort)
cboPort.AddItem vPort(lPort)
' If this is the port that was selected
' Remember the ListIndex, so it's selected in
' The DropList.
If vPort(lPort) = sPorts Then lIndex = lPort
Next
' Set the ListIndex to the Original Port
' So it's Selected when the list is Dropped.
cboPort.ListIndex = lIndex
End Sub
-
Feb 23rd, 2002, 09:33 AM
#3
Thread Starter
Hyperactive Member
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!!
-
Feb 25th, 2002, 12:49 PM
#4
Thread Starter
Hyperactive Member
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.
-
Feb 28th, 2002, 07:12 PM
#5
Thread Starter
Hyperactive Member
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.
-
Feb 28th, 2002, 07:25 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|