pop-up listbox in MDI application

farshizzo used Ask the Experts™

I'm trying to create an IDE with support for autocompletion similar to Visual C++. It's an MDI application that uses CRichEditView.  I created my own class CComplete that inherits from CListBox.  I'm able to popup the listbox and fill it with the appropriate words but I have a couple problems.  First off, my CComplete class does not respond to the Escape key.  I handle the WM_KEYDOWN message and it responds to every key except for VK_ESCAPE.  Second, if I set the CRichEditView as the parent of CComplete then the listbox will only be drawn inside the view and will be cutoff if it goes outside the views frame.  I fixed this by setting the main window as the parent, but then the list box wouldn't respond to mouse clicks.  Any ideas?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Looks like the CRichEdit is getting the VK_ESCAPE

 Try overiding ... CRichEdit's PreTranslateMessage(MSG* pMsg) (see Class Wizard)

 If a VK_ESCAPE comes in check if your Listbox is active and pass it over

 m_listMyListBox->SendMessage ( ... );
 or simply close the listbox.

 For the second part when you listbox is active try

 m_listMyListBox->SetCapture ();


william, your escape key solution worked, but only if I set the CRichEditView as the parent.  If I set the main window as the parent does that mean I have to handle the PreTranslateMessage in the main window and pass it on?
Also, when I SetCapture() in my CComplete class the mouse arrow would disappear.


I forgot to mention something. When I set the main window as the parent the list box doesn't respond to mouse clicks, but it does respond to the mouse wheel.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

I think it's the same problem the MainFrame is taking the mouse messages try forwarding these to the Listbox also.
It seems to me that the list should be a popup window not a child window so that it is not clipped (even to the main window).  However, a listbox must be a child window.  Have you considered creating a popup window and putting the listbox within that?
Thats right, the CListBox class doesn't respond to VK_ESCAPE, but it responds to WM_KEYUP. Add the WM_KEYUP event to your "CComplete class" and you see that it responds. Like this:

void CComplete::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
     // TODO: Add your message handler code here and/or call default
                   DestroyWindow( );
     CListBox::OnKeyUp(nChar, nRepCnt, nFlags);
I tested it and it works fine.


thanks nass, that worked great


aphillip, how would I go about putting the listbox within a popup window?
(Sorry I didn't reply earlier but I somehow missed getting email that said there had been followups in this thread.)

> how would I go about putting the listbox within a popup window?

You can create a popup window using CreateEx with style WS_POPUP.  In MFC I would just derive a class from CWnd (use "generic CWnd" as the base class in Class Wizard) and call CreateEx in the contructor.

Then create a CListBox using the above popup window as the parent passed to CListBox::Create().  Make it the same size as the popup window.

There may be example code on CodeGuru or CodeProject to do this.  There should at least be code that shows how to create a popup window.


aphillip, I tried the popup window and it works perfectly, I'll make sure to get you some points for you answer. thanks a lot!
> I tried the popup window and it works perfectly ...

glad I could help

> I'll make sure to get you some points for you answer

Don't worry about it.  I don't do it for the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial