Click here to Skip to main content
Click here to Skip to main content

Two Line Text Combobox

, 14 Dec 2011
Rate this:
Please Sign up or sign in to vote.
CComBoxEx is a simple MFC control derived from CWnd, it can display two line data

Introduction

CComBoxEx is a simple MFC control derived from CWnd. It can display two line text in combobox control.

Background

A few days ago, in one of my projects, I need one combobox control that could display two line text. Because the project time was tight, firstly I hoped that I could get some derived combobox controls from The Code Project website, but I got nothing. Finally, I decided to make this control by myself. Now I completed this combobox control, and it is not so bad for a VC++ beginner, so I decided to share this control with all CodeProject workmates. I did not have a lot of time to work on this control, so I had to code it in a simple manner, but it had to satisfy my requirements. It has some features as follows:

  1. It is simple, and easy to use.
  2. It is similar to the main functions of MFC combobox control.
  3. It can display two line text.

Now the combobox control is here. Enjoy and please help me with your invaluable notes, bugs reports, ideas, etc. that you think might improve the quality of this code.

Using the Code

Creating the Combobox Control

First, we need to create a two line text combobox control.

  1. Add ComBoxEx.h and ComBoxEx.cpp to your project.
  2. Add #include " ComBoxEx.h" to the top of header file of class in which you want to add this two line text control.
  3. Add a member variable of type CComBoEx.
  4. In your cpp file, use the Create method of the member variable to create a two line text combobox control.
    // create combobox control
    // to get combobox control position
    CStatic *pStatic = (CStatic *)GetDlgItem(IDC_COMBOBOX_STATIC);
    CRect rcStatic;
    pStatic->GetWindowRect(rcStatic);
    ScreenToClient(rcStatic);
    // to create combobox control
    m_ComboxEx.Create(rcStatic, this, 2000);
    
    // add data to combobox control
    CString str1, str2;
    str1 = "the first line data";
    str2 = "the second line data";
    m_ComboxEx.AddString(str1, str2);
    
    str1 = "the third line data";
    str2 = "the fourth line data";
    m_ComboxEx.AddString(str1, str2);
    
    str1 = "the fifth line data";
    str2 = "the sixth line data";
    m_ComboxEx.AddString(str1, str2);
    
    // set combobox current selected item
    m_ComboxEx.SetCurSel(1);
    
    // Get Combobox Control Current Selected Item
    m_ComboxEx.GetCurSel(); 
  5. Combobox control notify message

    When combobox control selects one item, combobox control's parent window would receive combobox control's notify message, you can do it like this.

    // first declare msg response function in parent window header, under
    //}}AFX_MSG and up DECLARE_MESSAGE_MAP(), just like this.
    	afx_msg LRESULT OnComboboxNotify(WPARAM wParam, LPARAM lParam);
    
    	// second map combobox msg, between 
             // BEGIN_MESSAGE_MAP and END_MESSAGE_MAP(), just like this.
    BEGIN_MESSAGE_MAP(CComboboxTextDlg, CDialog)
    	//{{AFX_MSG_MAP(CComboboxTextDlg)
    	ON_WM_SYSCOMMAND()
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_WM_CTLCOLOR()
    	//}}AFX_MSG_MAP
    	ON_MESSAGE(COMBOBOXEX_NOTIFYMSG, OnComboboxNotify)
    END_MESSAGE_MAP()
    
    	// finally, code combobox msg process function
    	LRESULT CComboboxTextDlg::OnComboboxNotify(WPARAM wParam, LPARAM lParam)
    	{
    		TRACE("Combobox CtrlID = %d, CurSelNo = %d\n", wParam, lParam);
    
    		return 1;
    	}

Next to Update

This Combobox control is simple, and there are many functions that need update.

  1. Support font setting

    Now this combobox control uses the default font, and I didn't implement font setting functions.

  2. Combobox auto size

    Combobox control can't auto size, when you use it, you must set control height and width to fit content, next I will implement it.

  3. Comboxlist support scroll bar

    Combobox control calculates comboxlist size according to combobox control' size. When combobox control items are very large, comboxlist sizes are larger than screen size, it can't display all items on screen.

  4. Combobox support item data

    You know MFC combobox control support information for every item, next to update this combobox control and support this function.

  5. Support dropdown style

    Now this combobox control does not support this style, next add to it.

History

  • 2011/11/22 Create combobox control

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

mengxz

China China
No Biography provided

Comments and Discussions

 
QuestionPossible in dot net Pinmembermayur csharp G14-Dec-11 21:54 
AnswerRe: Possible in dot net Pinmembermengxz14-Mar-12 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 Dec 2011
Article Copyright 2011 by mengxz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid