两个Combo box组合框控件关联xml中的数据

仅为初学者提供。

1、在xml文件中,有如下关系:

<Shops>
<Shop NO="0" Name="山东省">
<Units>
<Unit NO="0">济南</Unit>
<Unit NO="1">青岛</Unit>
<Unit NO="2">潍坊</Unit>
<Unit NO="3">烟台</Unit>
</Units>
</Shop>
<Shop NO="1" Name="湖南省">
<Units>
<Unit NO="0">长沙</Unit>
<Unit NO="1">张家界</Unit>
<Unit NO="2">益阳</Unit>
</Units>
</Shop>
</Shops>
2、在初始化函数中,添加如下代码

	
	xml.FindElem(_T("Shops"));
	xml.IntoElem();	
	while(xml.FindElem())
	{
		((CComboBox*)GetDlgItem(IDC_SHOP))->AddString(xml.GetAttrib(_T("Name")));
	}
	((CComboBox*)GetDlgItem(IDC_SHOP))->SetCurSel(0);
	xml.OutOfElem();

	xml.FindElem(_T("Shops"));
	xml.IntoElem();
	xml.ResetMainPos();
        xml.FindElem();
	xml.IntoElem();
	xml.FindElem(_T("Units"));
	xml.IntoElem();	
	while(xml.FindElem())
	{
		((CComboBox*)GetDlgItem(IDC_UNIT))->AddString(xml.GetData());
	}
	((CComboBox*)GetDlgItem(IDC_UNIT))->SetCurSel(0);
	xml.OutOfElem();
	xml.OutOfElem();
	xml.OutOfElem();

3、然后在第一个组合框控件的消息响应函数中添加如下代码,可以使第一个改变的同时,第二个对应的改变:

void CDlgUnit::OnSelchangeShop() 
{
	// TODO: Add your control notification handler code here
	((CComboBox*)GetDlgItem(IDC_UNIT))->ResetContent();

	CString strShop;
	int i=((CComboBox*)GetDlgItem(IDC_SHOP))->GetCurSel();
	((CComboBox*)GetDlgItem(IDC_SHOP))->GetLBText(i,strShop);
	
	CMarkup xml;//CMarkup是有关xml文件的解读,网上有文件,可以直接使用。
	xml.Load(GetCurDir()+_T("123.INI"));//123.INI是xml文件的名称。
	xml.ResetPos();
	xml.FindChildElem();
	xml.IntoElem();	

	xml.FindElem(_T("Shops"));
	xml.IntoElem();
	
	while(xml.FindElem()){
		if(xml.GetAttrib(_T("Name"))==strShop)
		{
			xml.IntoElem();
			xml.FindElem(_T("Units"));
			xml.IntoElem();	
			while(xml.FindElem()){
				((CComboBox*)GetDlgItem(IDC_UNIT))->AddString(xml.GetData());
			}
			((CComboBox*)GetDlgItem(IDC_UNIT))->SetCurSel(0);
			break;
		}		
	}	
}
以上仅供参考。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值