ListBox on EditView ...

Posted on 1998-10-08
Medium Priority
Last Modified: 2013-11-20
I ran the MFC Applicationwizard and used CEditView as base class instead of CView.
Now I want to attach a ListBox on it like in Visual Basic and Visual C++. That ListBox must foolow the caret position.
How can I do this?
BTW: I am novice in MFC and VC++ stuff.
Question by:ndb
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4

Expert Comment

ID: 1323092
1. Override PreCreateWindow for the CEditView.  In this make sure the WS_CLIPCHILDREN style is set

cs.style | = WS_CLIPCHILDREN

2. Add a member variable to the CEditView derived class
 CListBox  m_List

3. Override OnCreate for CEditView. Call m_List.Create to create the list box.  Make sure it has WS_CHILD style.  Make the list box visible using m_List.ShowWindow(SW_SHOW)

4. In Edit view, handle the reflected EN_SELCHANGE message (=EN_SELCHANGE).  This will tell you the selection/caret has moved within the edit control.  Use m_List.MoveWindow to move the list box

5. If the edit view has horizontal scrollbars you need to also handle =EN_HSCROLL, which will occur when the edit control is scrolled (even if caret doesn't move) and use m_List.MoveWindow

6. If the edit view has vertical scrollbar, handle also =EN_VSCROLL, similarly to #5

7. If you want the list box to disappear for a while, use m_List.ShowWindow(SW_HIDE)

8. If you want to the list box to reappear, use m_List.ShowWindow(SW_SHOW) ;

Expert Comment

ID: 1323093
Let me know if you consider this an answer to your Q, or one of the steps is not clear enough

Author Comment

ID: 1323094
How do I change the color and the borderstyle of that ListBox?
I know that the ListBox is ceated 'cause my mousepointer changes from chape but I can't see the ListBox.

Accepted Solution

Answers2000 earned 800 total points
ID: 1323095
1. To add a border
You can add a simple border, by setting the style (in m_List.Create call) to WS_BORDER.

2. To change color
i) The best thing to do is to derive a class from CListBox (use app wiz to generate a skeleton class derived from CListBox say CMyListBox)
ii) Change m_List variable from CListBox type to CMyListBox
iv) In CMyListBox add a member variable for a brush, say m_hBrush
v) In CMyListBox constructor create the brush to desired color, e.g.
      m_hBrush = CreateSolidBrush( RGB(0,0,128) ) ; // dark blue
vi) In CMyListBox destructor delete the brush, e.g.
   DeleteObject( m_hBrush)
vii)  In CMyListBox respond to =WM_CTLCOLOR message (see TN062 for more info on how this works).change the color and return the brush, e.g.
HBRUSH CMyListBox::CtlColor(CDC* pDC, UINT nCtlColor)
      pDC->SetBkColor( RGB(0,0,128) ) ; // dark blue
      pDC->SetTextColor( RGB(0,255,255) ) ; // cyan

      return m_hBrush ;

3. Also Make sure the list box has a reasonable size for width and height

4. Also make sure the list box contains some stuff.  For example after calling m_List.Create in the edit view, add a couple of strings, e.g.

m_List.AddString( "Hello" ) ;
m_List.AddString( "World" ) ;


Expert Comment

ID: 1323096
Typo in my prev comment

>> You can add a simple border, by setting the style (in m_List.Create call) to WS_BORDER.

should have said

You can add a simple border, by setting the style (in m_List.Create call) to include WS_BORDER. (styles are separated by |)

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question