Click here to Skip to main content
Licence 
First Posted 12 May 2001
Views 159,450
Downloads 3,115
Bookmarked 43 times

Adding a Combo Box to a Docking Toolbar

By | 12 May 2001 | Article
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.

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

About the Author

zhaque

Web Developer

Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow to let the combobox width is dynamic just like adrress bar in IE? Pinmemberzhouwz15:36 17 Sep '08  
GeneralResize PinmemberLameThought2:53 2 Apr '07  
GeneralCutting Toolbar in VC++ 7.1 Pinmembercsavie14:49 8 Aug '05  
Generaltryhtrjyt PinsussAnonymous19:03 7 Jul '05  
GeneralA handle to the MENU of a drop down button PinmemberAlex Evans13:32 16 Nov '04  
General32x32 toolbar buttons PinmemberDeian23:46 11 May '04  
GeneralRe: 32x32 toolbar buttons Pinmemberverinder_bindra13:34 16 May '04  
GeneralRe: 32x32 toolbar buttons PinmemberDeian13:43 16 May '04  
GeneralRe: 32x32 toolbar buttons Pinmemberdefrog4:59 25 Nov '04  
QuestionRe: 32x32 toolbar buttons Pinmember_Budda_21:57 3 Oct '07  
GeneralRe: 32x32 toolbar buttons PinmemberLJMiller17:59 24 Mar '06  
GeneralRe: 32x32 toolbar buttons PinmemberDeian4:48 27 Mar '06  
GeneralRe: 32x32 toolbar buttons Pinmemberlishuangyou17:44 21 Jun '07  
AnswerRe: 32x32 toolbar buttons Pinmember_Budda_22:24 3 Oct '07  
Generalevents in CListBox Pinsussnaghmehsemsar3:21 27 Feb '04  
Generalworks fine if you add combo at the end Pinmemberverinder_bindra16:28 10 Jan '04  
GeneralRe: works fine if you add combo at the end Pinmemberdefrog5:01 25 Nov '04  
GeneralAbout the size of the ComboBox PinmemberExceter18:31 21 Sep '03  
GeneralRe: About the size of the ComboBox PinsussAnonymous5:42 7 Oct '03  
QuestionHow to insert CListBox in toolbar and call notification of CListBox SelChange PinmemberSprinter1:24 31 Jan '03  
QuestionHow to insert two combos? PinsussAnonymous17:04 17 Oct '02  
AnswerRe: How to insert two combos? Pinmemberdefrog5:06 25 Nov '04  
GeneralProblems with putting scrollbar on the left or right PinsussJeroen de Haas23:46 5 Sep '02  
GeneralRe: Problems with putting scrollbar on the left or right PinmemberNishant S. Thorat20:57 27 Jun '04  
GeneralLimitation PinmemberSriram V1:57 7 Jun '02  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 May 2001
Article Copyright 2001 by zhaque
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid