65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 XML 作为字典的 ComboBox

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.50/5 (7投票s)

2002年10月25日

2分钟阅读

viewsIcon

81405

downloadIcon

843

创建一个新的组合框,可以使用 XML 中的字典自动填充。

引言

在某些情况下,我们需要一个组合框来显示一个供用户选择的项目列表。这些项目存储在一个带有身份代码的字典中。特别是当您使用数据库时,用户从下拉列表框中读取具有含义的内容;但它还需要返回该项目的代码。此控件简化了您使用 XML 时的选择。

字典的实现

我上次发布了一个 XML DOM 包装器,其中我将 CXMLFile 类包装起来,以便解析和使用 XML 更加容易。在这里,我又编写了另一个包装器,使用 CXMLFile 来表示字典。如果您找不到此示例的基类,您将在文章“Windows SDK 的 DOM 接口包装类”的源代码中找到该类。

字典的格式如下。

<?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> 

字典包含带有代码的项目。它们将在组合框中显示。组合框将自动填充自身。我们从 CXMLFile 派生一个新的类来解决这个问题。字典类包括用于搜索和其他维护用户字典的方法。以下是该类的头文件。

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();

};

生成组合框

为了使用上述构建的字典,我们从 CComboBox 作为基本控件派生一个新的组合框类。组合框将从给定的字典中填充。用户可以通过字典中的项目代码选择项目。新的组合框将自动返回当前所选项目的项目代码。以下原型定义解释了该类的实现。

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);
  
};

在大多数情况下,组合框用于对话框或表单中。最好为此新的组合框提供一个数据交换函数。

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);
   }
}

用法

使用这个类非常简单。像往常一样创建一个新的对话框模板。声明一个变量来接受返回值。在 DoDataExchange 中添加一行 DDX 来控制值。并在 OnInitDialog 函数中使用字典填充组合框。希望这能帮助到您。谢谢。

© . All rights reserved.