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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to stop combo box from dropping down list 2

Status
Not open for further replies.

Punchinello

Programmer
Apr 25, 2003
116
US
I have a combo box and I want to replace the default behavior when the DropDown event occurs with custom code. I can do this easily in other languages but cannot find the solution in VB. The custom code is not really important -- can someone tell me how to silently abort the dropdown behavior of a combobox?

Thanks
 
Could you not just set the enabled property of the combo box to false?
 
I think you can also try .....
form1.combo1.locked = true
 
vbrocks,

thanks for the reply. I don't want the control to be disabled or locked. What I want is for the combobox to look like a combobox (with the dropdown arrow) and let the user tab or click into it like normal. But when they click on the down arrow (triggering the _DropDown event), I want to prevent Windows from showing the list.

In Delphi, for example, I could use a method called Abort in any event handler and it would prevent the default action from progressing.

Consider the following code:
Code:
Private Sub MyComboBox_DropDown()
  MsgBox "See this message?"
End Sub
The message box pops up before the combo box's list appears but when you accept the message box, the list appears anyway. I want the list NOT to appear.
 
You can also try setting the style property to simple combo.
 
I do'nt understand what you'll do but their can be a solution

Private Sub ComboBox1_KeyDown(KeyCode As MSForms.ReturnInteger, Shift As Integer)
KeyCode = 27
End Sub

Private Sub ComboBox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
SendKeys "{Esc}"
End Sub


peterguhl@yahoo.de
 
OK i've forgotten:

The standart combobox doesn't have the mouseevent you must ocupate de Combobox from the MS Form 2.0 Object

peterguhl@yahoo.de
 
But it does have a DropDown event:

Private Sub Combo1_DropDown()
SendKeys "{ESC}"
End Sub
 
OK Your solution is better CCLINT

Now you must wait for your star from dhoernig

peterguhl@yahoo.de
 
Thanks poltergeist and cclint for replies. I should've mentioned that I already tried the SendKeys option but I rejected it because it results in 2 effects that I don't want:

1. The pick-list still appears and then quickly disappears
2. After execution of the DropDown event, the next mouse click anywhere on the form is ignored.

What I really want is functionality similar to the DateTimePicker control. This control appears just like a combo box but when the down arrow is clicked, instead of showing a pick list, it displays a calendar.

Probably my hopes of starting with a stardard combo box are not realistic for this endeavor.
 
I can understand #1. We could get around this by freezing the window with an API call, or placing Combo1.Locked = True in the Click event and Combo1.Locked = false after the SendKeys.

As for #2, yes you need to click twice.

I could give you code for a Hook (CallBack proceedure), but the Combo box needs the style 2, and this you wouldn't want.

Why not just use a text box and add a Image control (or a small picture box if you need the CausesValidation to kick in) with a bitmap of an arrow, or even a command button with a picture of an arrow setting it's Style property set to 2 - Graphical?

You could dump these onto a borderless frame to you can move them easily together, or even create an ActiveX Control.


 
CCLINT,

First, let me thank you with a star for going the extra mile with your suggestions.

My concern with a graphical arrow approach is that it doesn't survive the "multi-platform visual compatibility test" without a lot of work. Meaning that I would need to apply a WinXP graphical arrow and perimeter in that environment and Win98 graphics for earlier versions. And what if the user uses XP in classic mode?

I'm pretty sure at this point that I will have to create an ActiveX control -- I just haven't learned how yet.
 
I've been experimenting, and it can be done with a hook for the other styles, although not as cleanly as I'd like (and my current version suffers from the fault 2 mentioned above)
 
i feel a "proof of concept coming on"

go strongm, give us your code!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
To get the best response to a question, please check out FAQ222-2244 first
A General Guide To Excel in VB FAQ222-3383
 

Well, of course it could be done other uMsg IDs are being generated, so it can be trapped.
But I discarded t5he idea of persuing it a while ago because of it still suffering from other symptoms, and to persue further this seemed senseless, or should I say, too much effort (figuring out the right mixture) when a simple text box and button (along with your own drop-down portion created with a ListView or DataGrid if you want it to drop down) is easier and more effective.
You can see in some past examples, also of mine, in this forum where this was being done in order to produce drop downs like the one from MSForoms 2.0, or with even more features available, but it seemed to me in the end more effective and efficient to create the same using 3 other basic controls (and additional features and functionality are possible with minimal efforts).

And for the given purpose, I see no real need. Instead of using an arrow on the Button, use something like the search binoculars for this task...

But, go for it strongm, maybe we'll learn something else by this process, as often is the case when being persistant in finding new methods, and is often when you pull out your API tricks...
 
No, I think I'm reaching the same dead end. The VB Combobox is heavily subclassed, and it seems like too much work for what should be a simple result. I might give it another hour of my time later to see if any insights spring to mind, but I'm not holding out much hope.
 
Try this it seems to work... with two forms..

Private Sub Combo1_DropDown()
On Error GoTo handler:
SendKeys "{ESC}"
Err.Raise 51
Exit Sub
handler:
Resume hector:
Exit Sub
hector:
Form2.Show

End Sub
Private Sub Form_Load()
Form1.Combo1.AddItem ("Hog")
Form1.Combo1.AddItem ("Hog")
Form1.Combo1.AddItem ("Hog")
Form1.Combo1.AddItem ("Hog")
End Sub
 
First, thanks for all suggestions.

I took a stab at an earlier suggestion which was to use an Edit control (or a ComboBox with Style = 1) and add a PictureBox whose Paint event was used to draw the button and whose Click event was used in place of a ComboBox's DropDown event. My concern with this approach was that of drawing visually compatible "button" controls. (Although using a different graphic altogether is a good idea)

Anyway, there is an API call - DrawFrameControl - that will draw different button-style controls and, even though I will not be using it because it doesn't seem to incorporate XP styles, I'm posting the code for anyone else who might want to explore or use it further.
Code:
'Declare a rectangle type for use thoughout the project
Public Type TRect
  Left   As Long
  Top    As Long
  Right  As Long
  Bottom As Long
End Type

'Declare the function for use throughout the project
Public Declare Function DrawFrameControl Lib "user32" (ByVal hDC As Long, nRect As TRect, ByVal uType As Long, ByVal uStyle As Long) As Long

'Declare the constants used by uType and uStyle
'Others are available - these refer to combo-box buttons
Global Const DFC_SCROLL = 3
Global Const DFCS_SCROLLDOWN = 1

'Use a picture box's Paint event to draw the button
Private Sub pbComboBoxButton_Paint()
  DrawFrameControl (pbComboBoxButton.hDC, Rect(0, 0, 17, 17), DFC_SCROLL, DFCS_SCROLLDOWN)
End Sub

Note that the rectangle is measured in Pixels, not Twips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top