首    页 界面/窗口 网络/通讯 数据库 组件开发 图像/多媒体 NET/Web 其它技术 源码下载 资料下载 软件共享 软件外包 曲艺杂谈
栏目导航:  首    页  |  界面/窗口  |  组合框   


MFC中CCOMBOX组合框的使用


原作者:qsdnet    源出处:CSDN    发布者:施昌权    发布类型:转载    发布日期:2008-10-15

              

MFC中CCOMBOX组合框的使用

如何在MFC中使用CCOMBOX组合框?下面将用一个例子介绍CCOMBOX组合框控件常用的方法。

首先我们新建一个single类型的mfc.exe工程,在菜单栏中添加一个菜单项,如“CCOMBOX组合框”,双击后

可以在里面添加相关事件。

插入一个Dialog对话框,并添加一个类CDlgCombox,上面添加2个组合框,7个按钮。

详细代码如下:

// DlgCombox.cpp : implementation file
//

#include "stdafx.h"
#include "testvc.h"
#include "DlgCombox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgCombox dialog


CDlgCombox::CDlgCombox(CWnd* pParent /*=NULL*/)
  : CDialog(CDlgCombox::IDD, pParent)
{
  //{{AFX_DATA_INIT(CDlgCombox)
  //}}AFX_DATA_INIT
}


void CDlgCombox::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  //{{AFX_DATA_MAP(CDlgCombox)
  DDX_Control(pDX, IDC_EDIT1, m_EditName);
  DDX_Control(pDX, IDC_COMBO2, m_ComboxSex);
  DDX_Control(pDX, IDC_COMBO1, m_ComboxName);
  //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgCombox, CDialog)
  //{{AFX_MSG_MAP(CDlgCombox)
  ON_BN_CLICKED(IDC_BUTTON1, OnAddString)
  ON_BN_CLICKED(IDC_BUTTON3, OnGetCount)
  ON_BN_CLICKED(IDC_BUTTON4, OnGetWindowText)
  ON_BN_CLICKED(IDC_BTN_CLEAR, OnBtnClear)
  ON_BN_CLICKED(IDC_BTN_CLEAR2, OnBtnClearList)
  ON_BN_CLICKED(IDC_BTN_DELETE, OnBtnDelete)
  //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgCombox message handlers

void CDlgCombox::OnAddString() //插入或者添加一项
{
  // TODO: Add your control notification handler code here
  CString str;
  m_EditName.GetWindowText(str);
  int i=m_ComboxName.GetCount();
// m_ComboxName.InsertString(i,str );//如果在后面插入,请用
  UpdateData(FALSE);
   m_ComboxName.SetCurSel(0);


}

void CDlgCombox::OnGetCount()  //得到下拉列表中的项目总数
{
  // TODO: Add your control notification handler code here
  CString str;
     int i=m_ComboxName.GetCount();
 
     str.Format ("%d",i);
  str="下拉列表中一共有"+str+"项.";
  AfxMessageBox(str);
}

void CDlgCombox::OnGetWindowText() //得到被选择的文本
{
  // TODO: Add your control notification handler code here
  CString str;
  m_ComboxName.GetWindowText(str);
    CString strSex;
  m_ComboxSex.GetWindowText(strSex);
  str="您选择的姓名是"+str+",性别是"+strSex;
    AfxMessageBox(str);
}

void CDlgCombox::OnBtnClear() //清除被选定的文本,此法适合下拉列表中有值
{
  // TODO: Add your control notification handler code here
  m_ComboxName.SetEditSel(0, -1);
  m_ComboxName.Clear() ;
  UpdateData(FALSE);
}

void CDlgCombox::OnBtnClearList() //清除下拉列表
{
  // TODO: Add your control notification handler code here
  
   for (int i=0;i < m_ComboxName.GetCount();i++)
   {
   m_ComboxName.DeleteString( i);
  i--;
   }
  UpdateData(FALSE);
}

void CDlgCombox::OnBtnDelete() //清除被选定的文本
{
  // TODO: Add your control notification handler code here
  CString str;
// m_ComboxName.GetWindowText(str);
   m_ComboxName.SetWindowText("");
    UpdateData(FALSE);
}



关于我们 版权声明 广告服务 联系我们 友情链接 加入收藏
站长:施昌权    Email:scq2099yt@163.com    MSN:scq2099yt@live.cn    QQ:14046300    本站QQ群:67202409
Copyright © 2008     卓为VC(www.joyvc.cn)    All Rights Reserved    建议分辨率 1024×768
本站由施昌权制作维护
京ICP备09012297号