正文

如何在MFC中使用LISTBOX控件2009-08-19 00:48:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/shao/46819.html

分享到:

如何在MFC中使用LISTBOX控件。实例如下:

一。首先新建一个Single或者对话框类型的MFC.EXE工种。

二。插入一个Dialog,然后新建一个类CDlgList,我们可以利用这个控件显示项目,也可实现多选,得到选项总数等.其源程序如下:

// DlgList.cpp : implementation file
//

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

#include "DlgList.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDlgList dialog


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


void CDlgList::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgList)
DDX_Control(pDX, IDC_BUTTON1, m_Del);
DDX_Control(pDX, IDC_EDIT1, m_Edit);
DDX_Control(pDX, IDC_LIST1, m_Lst);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgList, CDialog)
//{{AFX_MSG_MAP(CDlgList)
ON_BN_CLICKED(IDC_BUTTON1, OnAddString)
ON_BN_CLICKED(IDC_BUTTON2, OnDel)
ON_BN_CLICKED(IDC_BUTTON5, OnClear)
ON_BN_CLICKED(IDC_BUTTON3, OnGetCount)
ON_BN_CLICKED(IDC_BUTTON4, OnGetCurSel)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgList message handlers

void CDlgList::OnAddString()
{
// TODO: Add your control notification handler code here
    CString str;
m_Edit.GetWindowText(str);
m_Lst.AddString(str);
   UpdateData(FALSE);
}

int CDlgList::DoModal()
{
// TODO: Add your specialized code here and/or call the base class

return CDialog::DoModal();
}

void CDlgList::OnDel()
{
// TODO: Add your control notification handler code here

   m_Lst.DeleteString(m_Lst.GetCurSel());
   UpdateData(FALSE);
}

void CDlgList::OnClear()
{
// TODO: Add your control notification handler code here
for(int i=0;i<m_Lst.GetCount();i++)
{m_Lst.DeleteString(i);i--;}
    UpdateData(FALSE);
}

void CDlgList::OnGetCount()
{
// TODO: Add your control notification handler code here
CString str;
str.Format("List中共有%d项",m_Lst.GetCount());
AfxMessageBox(str);
    
}

void CDlgList::OnGetCurSel()
{
// TODO: Add your control notification handler code here

// AfxMessageBox(str);
   CString str,strMess;
   for (int i=0;i < m_Lst.GetCount();i++)
   {
     str.Format(_T("item %d: 您的选择状态是%s\r\n"),i,m_Lst.GetSel( i ) > 0 ? _T("true") : _T("false"));
      afxDump << str;
   strMess+=str;
   }

AfxMessageBox(strMess);
}

阅读(5980) | 评论(1)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册