5 Replies - 1269 Views - Last Post: 22 November 2011 - 12:30 PM Rate Topic: -----

#1 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Cannot retract combo box dropdown list

Posted 19 November 2011 - 06:14 AM

A combo box is placed within a MSFlexGrid so that when I click on the grid, the dropdown list appears and I can make a selection. For that, I use SendMessage cbo.hwnd, CB_SHOWDROPDOWN, 1, 0& in the grid's Click event for when the first column is clicked, and it works. However, unless I make a selection from the list, I cannot click anywhere else on the grid to cause the list to retract. If I make a selection, the list retracts. If I make no selection, the list retracts only if I click outside the grid. I have tried SendMessage cbo.hwnd, CB_SHOWDROPDOWN, 0, 0& in the LostFocus event of the combo box but clicking somewhere else in the grid does not activate this event and the list keeps appearing. What is wrong?

Here is an image to clarify the situation:

Posted Image

If I click in the column "Description", the combo box remains as it is in the column "Part No.". I have also tried SendMessage cbo.hwnd, CB_SHOWDROPDOWN, 0, 0& in the grid's Click event for when the second column is clicked but it does not work neither.

I need to know how to make the list retract when I click anywhere else in the grid, without any selection made in the combo box. Thanks in advance.

[SendMessage is declared in the General section of the form, as follows:

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const CB_SHOWDROPDOWN = &H14F
]

This post has been edited by AN1554: 19 November 2011 - 01:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot retract combo box dropdown list

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Re: Cannot retract combo box dropdown list

Posted 21 November 2011 - 11:48 AM

You've done some nice work here. Now, you need to get the handle of the combobox and store it in a module-level (form-level) variable. Then, when you click elsewhere on the grid you can call SendMessage and use the stored window handle to close the box. You can also do this in Just do this in the grid's click event.

I haven't tested this (can't for a while), but it looks right to me. Suppose you give it a try and see what happens?

This should also work if you click on another combobox: just call the same routine for the comboboxes' click events and the grid's click event.
Was This Post Helpful? 0
  • +
  • -

#3 AN1554  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 130
  • Joined: 03-April 11

Re: Cannot retract combo box dropdown list

Posted 22 November 2011 - 05:03 AM

I do not know how to use handles, I got the code from here. But the code to retract the combo box does not seem to work. I have attached a simple project relating to the problem.

Click in a cell. Combo box drops down automatically. While the dropdown list is visible, try to click in another cell. It does not work. The dropdown list remains where it is. Now, retract the list manually. Click in another cell. Combo box drops down automatically from there.

The problem is that you first have to retract the list manually, then click in another white cell, then the combo box will appear in that other cell.

What I want instead is to be able to click in another cell without having to manually retract the list, i.e. the list must retract automatically.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,755
  • Joined: 26-March 09

Re: Cannot retract combo box dropdown list

Posted 22 November 2011 - 06:07 AM

You're already using the handle for the combobox (cbo.hwnd) in your code.

Try adding your Retract code at the beginning of the event, so you're telling it to retract, before moving it and forcing it to drop down.

I tried it in the ClickEvent, but for some reason it doesn't seem to work as expected as it wasn't picking up that a new cell had been selected...I got better results in the mousedown event.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Re: Cannot retract combo box dropdown list

Posted 22 November 2011 - 11:49 AM

(Now that I've said all the cool stuff below about how handles work, I have had a look at your first post again. From what I can see it should work. I'll get a look at it soon.)

AN, I get what you're saying, and what I suggested should work. Let me give you a bit of background. Every control is a "window" as far as the API is concerned. Some windows contain other windows (forms, frames), some don't. (A window contained by another window is a "child window.") So, each of your combo boxes is a window, and so is your grid.

Windows keeps track of a "handle" to each window instance on your screen. VB keeps track of those in the hWnd property. Keep in mind that if you stop a program and rerun it again, the handles will all be different, because you are looking at new instances of the controls.

Now, you really do know how to use handles. When you send a message to a window, using the SendMessage function, all you're doing is this: Send a message. Where? Here's a handle. What message? Here's a constant. Need any parameters? (You can have up to two, and the number is set by the type of message that you're sending.) Yes, I need one, a 1 or a 0, depending on whether I want to open or close the dropdown.

Ok, so it follows that each time you send a 1 to a combo box, you should store the handle to a variable. Then, each time you send another one, you can send a 0 to the last one, send a 1 to the new one, and there you are.

This post has been edited by BobRodes: 22 November 2011 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,979
  • Joined: 19-May 09

Re: Cannot retract combo box dropdown list

Posted 22 November 2011 - 12:30 PM

Thinking a little more about this. It sounds like there are some funky things about when the click event actually fires when clicking on the grid. Can you msgbox the click event and see if it's firing when you expect it to?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1