Click here to Skip to main content
15,919,028 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 231.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

 
GeneralLimitation Pin
Sriram V7-Jun-02 1:57
Sriram V7-Jun-02 1:57 
GeneralRe: Limitation Pin
sdasec5-Feb-04 4:26
sdasec5-Feb-04 4:26 
QuestionHow to use Drop Down Arrow to show a wnd, just as what the arrow by Undo-button does in Visual Studio? Pin
msvcna26-Mar-02 16:21
msvcna26-Mar-02 16:21 
GeneralAssertion failure Pin
4-Oct-01 3:27
suss4-Oct-01 3:27 
GeneralRe: Assertion failure Pin
defrog25-Nov-04 5:03
defrog25-Nov-04 5:03 
GeneralAssertion failure Pin
4-Oct-01 3:26
suss4-Oct-01 3:26 
QuestionHow to respond to Windows messages? Pin
2-Jul-01 16:58
suss2-Jul-01 16:58 
AnswerRe: How to respond to Windows messages? Pin
elbitos2-Apr-03 22:21
elbitos2-Apr-03 22:21 
Unsure | :~ I had the same problem with a slidder, but the principle is the same.

1) Create a new Combo class, CDockCombo for instance, derived from CComboBox.
2) Manually add all the windows message of CComboBox you want to use.
3) As your combo box is docked into a toolbar, the parent of CDockCombo is the toolbar. So to enable you to declare your control in the CMainFrame, which is less complicated, you are just going to send messages to the CMainFrame each time an event is trigged in your CDockCombo.

4) In CDockCombo, declare as private: CWnd *pWndMsgParent;
5) In the constructor of CDockedCombo, set pWndMsgParent = NULL;


6) Create a methode in CDockCombo as follows:
void CDockComb::SetWndMsgParent(CWnd *pWnd)
{
m_pWndMsgParent = pWnd;
}

7) When you create your CMainFrame, or cherever you declare your CComboBox, do the following call to say CDockedCombo who to send the messages to.
CWnd *pWnd = this;
m_MyDockedCombo.SetWndMsgParent(pwnd);

8) Create a windows messages for your event.
The best is to register it...so in the header of CDockCombo.h,
write, outside the class declaration:
static const UINT wm_ChangeSelCombo = RegisterWindowMessage( "WM_CHANGE_SELCOMBO" );

The name of both the message and the string are up to you.

9) when the event is trigged in CDockCombo, send the message:
if(m_pMsgParentWnd)
m_pMsgParentWnd->SendMessage(wm_ChangeSelCombo);
This event must be one declared in CWnd or CComboBox. Otherwise it will never be called. It works well for the event of CWnd, ex.:OnHScroll(...) in my case, for specific CCombo events, give it some thoughts Smile | :)

10) Finally, receive it in CMainFrame:
10-1) In the header of CMainFrame.h, declare the method that will respond to the event.
afx_msg void OnSelchangeMyDockCombo(); // name up to you
10-2) in CmainFrame.cpp

// Link the message ID and the function that will finally respond to your event.
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)

ON_REGISTERED_MESSAGE( wm_ChangeSelCombo, OnSelchangeMyDockCombo)
//}}AFX_MSG_MAP

END_MESSAGE_MAP()

....and
void CMainFrame::OnSelchangeMyDockCombo()
{
Here you will know the combo box selection has chaged!!!
}


It's seems a bit long but it works well and takes 10 more times to explain than to do it.Smile | :) Smile | :)

GeneralRe: How to respond to Windows messages? Pin
ohtah31-Mar-05 17:52
ohtah31-Mar-05 17:52 
AnswerRe: How to respond to Windows messages? Pin
Member 5435591-Oct-05 20:52
Member 5435591-Oct-05 20:52 
GeneralAccessing the comboxbox Pin
14-May-01 17:58
suss14-May-01 17:58 
GeneralRe: Accessing the comboxbox Pin
14-May-01 20:55
suss14-May-01 20:55 
GeneralRe: Accessing the comboxbox Pin
16-May-01 4:44
suss16-May-01 4:44 
GeneralRe: Accessing the comboxbox Pin
17-Jul-01 23:33
suss17-Jul-01 23:33 
GeneralRe: Accessing the comboxbox Pin
BobMatura6-Nov-01 5:27
BobMatura6-Nov-01 5:27 
GeneralRe: Accessing the comboxbox Pin
elbitos2-Apr-03 1:11
elbitos2-Apr-03 1:11 

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.