Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling combo box drop up event

Status
Not open for further replies.

folo

Programmer
Oct 15, 2001
3
HR
Hy,

Is posibile to handle when the dropdown list of a combo box
hide and no value is selected?

Thanx.
 
I am not real clear as to what you are looking to do with the items in the combo's list but give this a try. Drop a combobox and a command button on a form then add this code:

Option Explicit

Private Sub Command1_Click()
Dim iY As Long

For iY = 0 To Combo1.ListCount - 1 'Print each List item in combo1 to the form
Print Combo1.List(iY)
Next
End Sub

Private Sub Form_Load()
Dim iX As Long

For iX = 0 To 10
Combo1.AddItem "Item " & CStr(iX)
Next
Combo1.Text = Combo1.List(0)
End Sub

Is that what you are looking for?

If you choose to battle wits with the witless be prepared to lose.
[machinegun][hammer]

[cheers]
 
Sorry for my English because it is not my mother tongue.

The problem is that I'm writing an OCX control and I have one textbox, one combo (witch is hidden under text box) and one button

When I click on button I set focus to the combo and send key F4 that open the dropdown list of the combo box. If I change something in list event combo_click is raised so I can handle that and move focus back to text box.

Problem is that when dropdown list of a combo box is visible, and I don't select anything from it and then click to command button no event is raised.
I'm wondering if there is some API function or something what can catch that event

Thanks

p.s. Here is sample code:
Code:
'Place one combobox, one textbox and one command buton on the form and paste this code


Option Explicit



Private Sub Combo1_Click()
    Text1.Text = Combo1.Text
    Text1.SetFocus
End Sub

Private Sub Command1_Click()
    Combo1.SetFocus
    SendKeys "{F4}"
End Sub

Private Sub Form_Load()
    Combo1.AddItem "Test 1"
    Combo1.AddItem "Test 2"
    Combo1.AddItem "Test 3"
    Combo1.AddItem "Test 4"
End Sub
 
I think what he is asking, is that if the user drops down a combo box, and then does not select anything and the combo box closes ( because the user clicked outside the box ), then he would like to trap this in code.

I believe the only way to do this would be to use the API, and hook the message that is sent to "close" the drop down section of the box, and then check the text value of the control to see if there is anything in it.

Robert
 
You could also just check in the LostFocus event of the combobox. When the user clicks outside of the control, this event will get triggered, then you can look at the combobox and see if anything was selected or not.
 
In that case you could probably catch it in the LostFocus or Validate event. Otherwise, the click event should fire even if a selection is not made. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Click event only fires if you click on the combo box list, not outside it.

LostFocus only halfway fixes it. If the Combo box is dropped down, and you click outside the box on the form or another control, the box will close, but it still maintains focus. You have to click a second time to change the focus to another control.

If you can live with this, then LostFocus will be OK.

Robert
 
TheVampire: I do not know about the combo that you are using, but mine works with the Click event, and Validate event every time....even if I switch to another program.
When I leave the drop down open, and switch to another program, the click event fires (provided the user has moved the highlighted item with the mouse or arrows, but has not clicked to actually selet the item).
This is if of course you have moved off of the default item that is selected when the combo opens. And if not, then this is also easy to determine, with a boolean variable or static variable, or a variable that gets set when the item is selected.

There are some sub classing example in this forum for the combo already, some which I also have posted, also one with a user defined drop-down list so "missing" properties can be used. So, I am just saying that I believe there is an easier way that may be prevent some problems, and crashes, for the less experienced programer. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 

Maybe this is being made more complicated than needed:

Option Explicit
Private m_bCmbSelected As Boolean

Private Sub cmbData_GotFocus()
m_bCmbSelected = False
End Sub

Private Sub cmbData_Validate(Cancel As Boolean)
m_bCmbSelected = True
End Sub


Look Mom, no hooks:
And to automatically cause the drop-down list to open (even though I try to avoid the SendKeys):

Private Sub OpenCombo()
cmbData.SetFocus
'show the drop-down list
SendKeys "%{DOWN}"
End Sub

[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
CCLINT,

When I tried it, I was not moving the mouse over the list and highlighting an item. Therefore the click event was not firing. You are correct in that it will fire if an item has been highlighted in the list but not clicked on. ( and the ListIndex will return -1, showing no item selected ). This was done with a combo that had two items in the list, but no default item.

( Personally though, I would not want the click event to fire unless the user actually clicked on a selection. Why does it do this? )

But this goes back to the original question, which was how to detect the drop up event *if no value was selected*?

When I tested it, the Validate event works the same way as the LostFocus does. It only fires after the *second* click outside of the combo box and the box has lost focus, not directly after the closing of the dropdown.

I understand what you mean about preventing crashes, and doing this an easier way. If it will work OK for them to wait for the lostfocus or validate event, then that's fine. But I maintain that the only way to detect 100% of the time the actual closing of the dropdown at the instant it happens, is to use the API. I did not see your earlier information on this forum about subclassing, so I directed them to what I had seen.

Robert
 
TheVampire:

Yes, of course.

But then a boolean should work. Either and item has been selected and it is True, or not and it is false.

But that method was being a little ignorant on my part.
The ListIndex, as you say, returns -1 when nothing has been selected in a ComboBox.

If Combo1.SelectedItem = -1 Then
'Nothing selected
End If

And a DataCombo returns Null:

If IsNull(DataCombo1.SelectedItem) Then
'Nothing selected
End If

So, forget the boolean.
[/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top