CCombobox enhancement to help with Text and item data

Download Source Code


The purpose of TComboBox is to make life easier for programmers when dealing with comboboxes that need to store a string, integer, etc. value as the code for any given string that is displayed. The "code" that I am referring to is the item data of the combobox itself (see the SetItemData() member function of CComboBox).

For example:

	// Header file contains entry for the TComboBox member variable.
	CTCombobox myCombo;
	CString m_csCodeOneStr;

 
	// Implementation file contains initialization of the combobox
	// Standard combobox method:
	CMydialog::OnInitDialog()
	{
		...

		// Special note: We have to use a member CString object so that 
		// when the string goes out of scope (because the OnInitDialog() 
		// function ends), the code string will still exist.
		m_csCodeOneStr = "Code1";

		DWORD dwCode = &m_csCodeOneStr;
		int nComboEntry;
		nComboEntry = myCombo.AddString("Here's one");

		if (nComboEntry != CB_ERR)
			myCombo.SetItemData(nComboEntry, dwCode);

		...
	}
	// Implementation file contains initialization of the combobox
	// TComboBox method:
	CMydialog::OnInitDialog()
	{
		...

		MyCombo.AddEntry("Here's one", "Code1");

		...
	}

You can now use the GetCode() method of TComboBox to retrieve the code for any item (or the currently selected item). This is of course instead of having to typedef the DWORD parameter over into a CString.

For example:

	CString csCodeValue;
	myCombo.GetSelectedCode(csCodeValue);

or,

	myCombo.GetCode(0, csCodeValue);

Last updated: 26 July 1998



Comments

  • How can i add/store data(e.g dates) in the Combo box

    Posted by Legacy on 06/05/2003 12:00am

    Originally posted by: Ananias Kgabo

    Hellow there,

    My name is Kgabo and the problem i'm facing right now is the use of Combobox.WE as a Project team,we are in position where we must make use of the ComboBox in our Project.

    Can you please help with whatever related infomation(ComboBOX)

    You can send it to my e-mail ani@webmail.co.za.
    I'll be thankful Should you give a helping hand.
    you'll be saving the World too.

    Reply
  • DWORD problems

    Posted by Legacy on 05/31/2000 12:00am

    Originally posted by: Dave

    well its said that the DWORD assignment of the CString variable does not work and so a pointer must be used...
    but i cant get the thing to work...ie

    DWORD *mydata = &mystring.

    m_mycombo.SetItemData(0, *mydata)

    can anyone help? i also will need help to use GetItemData since it returns void*????

    Reply
  • DWORD dwCode = &m_csCodeOneStr; error

    Posted by Legacy on 04/25/2000 12:00am

    Originally posted by: bae woo sung

    DWORD dwCode = &m_csCodeOneStr; 
    
    

    this statement is error..

    i don't know why error happens ..

    Can you know me the reason ?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds