Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC

Adding a Combo Box to a Docking Toolbar

Rate me:
Please Sign up or sign in to vote.
4.12/5 (15 votes)
12 May 20011 min read 230.2K   5.5K   48   40
This article demonstrates an easy way to add a Combo box to a docking tool bar.

Introduction

This article demonstrates an easy way to add a Combo box to a docking tool bar. I was needed to add a edit control to the toolbar for my dBase Explorer project (visit my web site for detail information: http://www.codearchive.com/~dbase/). Although, I found some of the articles on it in the Code Project site, most of them are difficult to implement, and you need to add a lot of code or even a new class. This article shows how you can add a combo box to a toolbar only by adding a few lines of code.

Image 1

In order to add a combo box to a toolbar, you need to declare a member variable type CComboBox to the CMainFrame class as shown below:

//
// Any source code blocks look like this
class CMainFrame : public CFrameWnd
{
	
protected: // create from serialization only
	CMainFrame();
	DECLARE_DYNCREATE(CMainFrame)

protected: // control bar embedded members
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
CComboBox m_comboBox;

...
};

You also need to create a place holder icon in the toolbar for the combo box. An Id should be assigned to the place holder icon by double clicking on it, for example, in my case ID_COMBO was assigned to the place holder. Then by calling the Create function, you create the combo box in the toolbar as shown below:

//
if(!m_comboBox.Create(CBS_DROPDOWNLIST | CBS_SORT | WS_VISIBLE |
		WS_TABSTOP | WS_VSCROLL, rect, &m_wndToolBar, ID_COMBO))
	{
		TRACE(_T("Failed to create combo-box\n"));
		return FALSE;
	}

 

The complete listing of the OnCreate function is given below:

//
//
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	CRect rect;
	int nIndex = m_wndToolBar.GetToolBarCtrl().CommandToIndex(ID_COMBO);
	m_wndToolBar.SetButtonInfo(nIndex, ID_COMBO, TBBS_SEPARATOR, 205);
	m_wndToolBar.GetToolBarCtrl().GetItemRect(nIndex, &rect);
	rect.top = 1;
	rect.bottom = rect.top + 250 /*drop height*/;
	if(!m_comboBox.Create(CBS_DROPDOWNLIST | CBS_SORT | WS_VISIBLE |
		WS_TABSTOP | WS_VSCROLL, rect, &m_wndToolBar, ID_COMBO))
	{
		TRACE(_T("Failed to create combo-box\n"));
		return FALSE;
	}
	m_comboBox.AddString("Toolbar Combobox item one");
	m_comboBox.AddString("Toolbar Combobox item two");
	m_comboBox.AddString("Toolbar Combobox item three");
	m_comboBox.AddString("Toolbar Combobox item four");
	m_comboBox.AddString("Toolbar Combobox item five");
	m_comboBox.AddString("Toolbar Combobox item six");

	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);
	return 0;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to let the combobox width is dynamic just like adrress bar in IE? Pin
zhouwz17-Sep-08 15:36
zhouwz17-Sep-08 15:36 
GeneralResize Pin
Try2-Apr-07 2:53
Try2-Apr-07 2:53 
GeneralCutting Toolbar in VC++ 7.1 Pin
csavie8-Aug-05 14:49
csavie8-Aug-05 14:49 
Generaltryhtrjyt Pin
Anonymous7-Jul-05 19:03
Anonymous7-Jul-05 19:03 
GeneralA handle to the MENU of a drop down button Pin
Alex Evans16-Nov-04 13:32
Alex Evans16-Nov-04 13:32 
General32x32 toolbar buttons Pin
Deian11-May-04 23:46
Deian11-May-04 23:46 
GeneralRe: 32x32 toolbar buttons Pin
verinder_bindra16-May-04 13:34
verinder_bindra16-May-04 13:34 
unfortunately.. I tried a lot and then gave up and removed the ListCntrl from the tool bar. I wanted to show the toolbar with text on buttons along with icon. even with 16*16 it doesn't show seperator properly in that case.

verinder

verinder
GeneralRe: 32x32 toolbar buttons Pin
Deian16-May-04 13:43
Deian16-May-04 13:43 
GeneralRe: 32x32 toolbar buttons Pin
defrog25-Nov-04 4:59
defrog25-Nov-04 4:59 
QuestionRe: 32x32 toolbar buttons Pin
_Budda_3-Oct-07 21:57
_Budda_3-Oct-07 21:57 
GeneralRe: 32x32 toolbar buttons Pin
LJMiller24-Mar-06 17:59
LJMiller24-Mar-06 17:59 
GeneralRe: 32x32 toolbar buttons Pin
Deian27-Mar-06 4:48
Deian27-Mar-06 4:48 
GeneralRe: 32x32 toolbar buttons Pin
lishuangyou21-Jun-07 17:44
lishuangyou21-Jun-07 17:44 
AnswerRe: 32x32 toolbar buttons Pin
_Budda_3-Oct-07 22:24
_Budda_3-Oct-07 22:24 
Generalevents in CListBox Pin
snaghmeh27-Feb-04 3:21
snaghmeh27-Feb-04 3:21 
Generalworks fine if you add combo at the end Pin
verinder_bindra10-Jan-04 16:28
verinder_bindra10-Jan-04 16:28 
GeneralRe: works fine if you add combo at the end Pin
defrog25-Nov-04 5:01
defrog25-Nov-04 5:01 
GeneralAbout the size of the ComboBox Pin
Exceter21-Sep-03 18:31
Exceter21-Sep-03 18:31 
GeneralRe: About the size of the ComboBox Pin
Anonymous7-Oct-03 5:42
Anonymous7-Oct-03 5:42 
QuestionHow to insert CListBox in toolbar and call notification of CListBox SelChange Pin
Sprinter31-Jan-03 1:24
Sprinter31-Jan-03 1:24 
QuestionHow to insert two combos? Pin
Anonymous17-Oct-02 17:04
Anonymous17-Oct-02 17:04 
AnswerRe: How to insert two combos? Pin
defrog25-Nov-04 5:06
defrog25-Nov-04 5:06 
GeneralProblems with putting scrollbar on the left or right Pin
Jeroen de Haas5-Sep-02 23:46
Jeroen de Haas5-Sep-02 23:46 
GeneralRe: Problems with putting scrollbar on the left or right Pin
Member 12299827-Jun-04 20:57
Member 12299827-Jun-04 20:57 
GeneralLimitation Pin
Sriram V7-Jun-02 1:57
Sriram V7-Jun-02 1:57 

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

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