MFC中CCOMBOX组合框的使用--源代码

来源:互联网 发布:苏州园区软件培训 编辑:程序博客网 时间:2024/06/04 17:59

如何在MFC中使用CCOMBOX组合框?下面将用一个例子介绍CCOMBOX组合框控件常用的方法。
              
首先我们新建一个single类型的mfc.exe工程,在菜单栏中添加一个菜单项,如“CCOMBOX组合框”,双击后
可以在里面添加相关事件。
插入一个Dialog对话框,并添加一个类CDlgCombox,上面添加2个组合框,7个按钮。
详细代码如下:
       // DlgCombox.cpp : implementation file
       #include "stdafx.h"<BR>
       #include "testvc.h"<BR>
       #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)<BR>
              {
               
              //{{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 &lt; m_ComboxName.GetCount();i++)
                
              {
              m_ComboxName.DeleteString(   i);
               
              i--;
                
              }
               
              UpdateData(FALSE);
              }
             void CDlgCombox::OnBtnDelete() //清除被选定的文本
              {
               
              //   TODO: Add your control notification handler code here<BR>
               
              CString   str;
              // m_ComboxName.GetWindowText(str);
                
              m_ComboxName.SetWindowText("");
                   
              UpdateData(FALSE);
              }

原创粉丝点击