Click here to Skip to main content
Click here to Skip to main content

A ComboBox using XML as dictionary

, 24 Oct 2002
Rate this:
Please Sign up or sign in to vote.
Create a new combobox which can fill itself automatically using a dictionary in XML
<!-- Add the rest of your HTML here -->

Introduction

Under some circumstances, we needed a combobox to display a list of items for the user to select. These items are stored in a dictionary with an identity code. Especially if you are using a database, user reads the content in the dropdown list box with meaning; but it also needs to return a code for that item. This control simplifies the selection while you are using XML.

Implementation of Dictionary

I posted a XML DOM wrapper last time , in which, I wrapped a CXMLFile class to parse and make it is easy for using XML. Here I wrote another wrapper to represent the dictionary using CXMLFile. If you cannot find the base class of this example, you will find that class in the source code of the article "Wrapper class for DOM interface of windows SDK".

The dictionary has the following format.

<?xml version="1.0" encoding="gb2312"?> 
<dicts> <dictionary>card_type 
<class_id>1</class_id> <dictitem> <code>0</code> 
<itemname>normal</itemname> </dictitem> <dictitem> 
<code>1</code> <itemname>abnormal</itemname> </dictitem> 
<dictitem> <code>2</code> <itemname>another</itemname> 
</dictitem> </dictionary> </dicts> 

The dictionary contains items with a code . They will be displayed in the combobox. The combobox will automatically fill by itself. We derive a new class from CXMLFile to solve the problem. The dictionary class includes the method for searching and other methods to maintain a dictionary by user. The following is the header for this class.

class CXMLDict : public CXMLFile  
{
public:
  /*
  Get the dictitem from a given dictionary using the code of item.
  IXMLDOMNode *node : the node represents dictionary
  Int Code:   the code for dictionary item
    CString &item : the result string to be find out from dictionary.
*/
  BOOL GetDictItem ( IXMLDOMNode * node , int code , CString &item);
/*
   Get how many dictionary stored in the current dictionary files.
*/
  BOOL GetNumOfDicts(long &len);
/*
  Get number of Items in the given dictionary.
*/
  BOOL GetNumOfItems( IXMLDOMNode * dict, long &num);
/*
  Get the ith dictitem’s code and content from current dictionary.
  IXMLDOMNode *dict;  the current dictionary
int i ; Which item will be fetched.
CString &item : the name of item 
long &code : the code of dict item
*/
  BOOL GetDictItem(IXMLDOMNode *dict,int i, long &code, CString & item);
/*
  Each dictionary has a classification number . 
  this function get the classification from current dictionary.
*/
  BOOL GetDictClass( IXMLDOMNode * dict, long & id );
/*
  Add new dictionary item into current dictionary.
*/
  BOOL AddDictItem(IXMLDOMNode *dict, long id , const CString & name);
/*
   Add a new dictionary into current dictionary list 
  with given name and classification code.
*/
  BOOL AddDictionary(const CString & name , const CString &cls, 
    IXMLDOMNode **dicts);
/*
  Load dictionary from a given XML strings of XML segments.
*/
  BOOL LoadStrDictionarys(const CString &xmlDicts);
/*
  Load dictionaries from a file.
*/
  BOOL LoadDictionarys( const CString & strPath);
/*
  The auxiliary functions help the class get the header 
  of all dictionaries in the current dictionaries’ list.
*/
  BOOL FindDicts();
// The dictionary parsed from a file or XML string.
  IXMLDOMNode * m_pDicts;
/*
  Initialize the dictionary.
*/
  BOOL InitDicts();
/*
  get the dictionary with given name.
*/
  BOOL GetDict(const CString &strDict, IXMLDOMNode **node);
  CXMLDict();
  virtual ~CXMLDict();

};

Producing the comboxbox

In order to using the dictionary construct from above , we derive a new combobox class from CComboBox as base control. The Combobox is filled from given dictionary. The user can select the item by item code in dictionary. The new combobox will return the currently selected item's item code automatically. The following prototype definition explains the implementation of this class.

class CDictComboBox : public CComboBox
{
// Construction
public:
  CDictComboBox();
  virtual ~CDictComboBox();

  // Get the item’s code in the dictionary of current selected item.
  BOOL GetSelData(long &data);
  // Find the item’s index in the combobox using the 
  // item’s code in the dictionary
  long FindItemByData( long data);
  // Select the item in the combobox using the item’s code in the dictionary.
  BOOL SelectItemByData( long data);
  // Fill the combobox using the dictionary has given name.
  BOOL LoadDict( const CString & name , CXMLDict *dict);
  
};

Under most circumstances, the combobox is used in a dialog or form . It is better to provide a Data Exchange function for this new combobox.

void PASCAL DDX_CBDictData( CDataExchange * pDX, int nIDC, long & data)
{
  // get data from controls
  CDictComboBox * box = (CDictComboBox*) CWnd::FromHandle (
    pDX->PrepareCtrl(nIDC)); 
   if ( pDX->m_bSaveAndValidate )
   {
     data= CB_ERR;
     box->GetSelData (data);
   }
   else
   {
    box->SelectItemByData (data);
   }
}

Usage

To use this class is very simple. Create a new dialog template as usually. Declare a variable that will accept the return code . Add a line of DDX in DoDataExchange for controlling the value. And fill the combobox using dictionary in the OnInitDialog function. Hope this will help you. Thank you .

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

Johnson Zhou
Software Developer (Senior)
China China
I'm write program from 1990. My research field is CAG,CAD and Image processing. I select C/C++, ASP, Java, XML as my usaully developing tools. Occasional , write code in Delphi and VB. I'm using Visual C++ from 1996. If you have anything unclear, e-mail to :zhou_cn123@sina.com Software Engineering and CAD is my mainly research program.
 
You also can reach me on msn: zhoujohnson@hotmail.com

Comments and Discussions

 
GeneralLimited data value PinmemberPaul Wolfensberger25-Oct-02 6:56 
Generalzzzzz PinsussPete Toms25-Oct-02 5:19 
GeneralRe: zzzzz PinmemberRon Heller25-Oct-02 5:57 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Oct 2002
Article Copyright 2002 by Johnson Zhou
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid