Click here to Skip to main content
15,881,840 members
Articles / Desktop Programming / MFC

Creating an Input for a Console using a Combo Box

Rate me:
Please Sign up or sign in to vote.
3.89/5 (4 votes)
25 Jan 2005CPOL3 min read 48.5K   1.1K   29  
A simple way to use a combo box for an input field for a console
/*
	ConsoleCombo.cpp
	By: Kevin Bond
	kevinbond@gmail.com
	www.mechdesk.ath.cx

	Use and distribute freely.  
	Please don't remove my name and don't take credit for my work. You may edit my code
	as long as you don't remove my name.

	Send comments, questions or bugs to kevinbond@gmail.com 
	or visit my site www.mechdesk.ath.cx
*/

/*Instructions:	This class is derived from the CComboBox class.  To use this you must
				derive a class from this and override the virtual ParseCommand() function
				with commands to be initiated when the user presses the enter key.  This
				derived class is the class you use for the CComboBox(s) in your application.
*/

// ConsoleCombo.cpp : implementation file
//

#include "stdafx.h"
#include "demo.h"
#include "ConsoleCombo.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CConsoleCombo

CConsoleCombo::CConsoleCombo()
{
	//set initial position
	m_nPos = -1;
}

CConsoleCombo::~CConsoleCombo()
{
}


BEGIN_MESSAGE_MAP(CConsoleCombo, CComboBox)
	//{{AFX_MSG_MAP(CConsoleCombo)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConsoleCombo message handlers

BOOL CConsoleCombo::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class

	if (pMsg->message == WM_KEYDOWN)
	{
		CString str;				
		int nVirtKey = (int) pMsg->wParam;

		
		switch (nVirtKey)
		{
		//checks if up arrow key is pressed
		case VK_UP:
			
			//if this is first position save current text
			if (m_nPos == -1)
			{
				GetWindowText(str);
				m_current = str;
			}

			
			//makes sure an invalid position isn't going to be set
			if (m_nPos + 1 < GetCount())
			{				
				//increase position
				m_nPos++;
				//get text at current position display it highlighted
				GetLBText(m_nPos, str);
				SetWindowText(str);
				SetEditSel(0, str.GetLength());									
				
			}

			
			return TRUE;
		
		case VK_DOWN:
			
			//if going back to bottem restore previously entered text
			if (m_nPos - 1 == -1)
			{
				SetWindowText(m_current);
				SetEditSel(m_current.GetLength(), m_current.GetLength());
				m_nPos = -1;
			}
			
			if (m_nPos - 1 >= 0)
			{
								
				//decrease position
				m_nPos--;
				//get text at current position display it highlighted
				GetLBText(m_nPos, str);
				SetWindowText(str);
				SetEditSel(0, str.GetLength());								
				
			}
			return TRUE;
		
		//if enter key is pressed do following
		case VK_RETURN:
			
			GetWindowText(str);
			
			//make sure there is something input
			if (str != "")
			{			
				//add string to the bottem of the list
				InsertString(0, str);
				m_nPos = -1;
				SetWindowText("");

				//function that must be customized for each program				
				ParseCommand(str);

			}
			break;
		
		}

		
		
	}
	
	return CComboBox::PreTranslateMessage(pMsg);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Canada Canada
I am 20 years old and I live in Kitchener, Ontario. I have been programming since I was 10 years old. I started with QBasic, then up to Visual Basic, and finally C++. I am an avid programmer of Windows as well. Besides that, I am also interested in automation, PLCs, and electronics. I will be attending Conestoga College in Sept. 2005 for their Mechanical Engineering: Automation and Robotics program. I hope to someday have a job where I can incorporate my skills with programming Windows with the skills I learn in that program.

Comments and Discussions