现在的位置: 首页 > 综合 > 正文

CVSListBox类的扩展使用

2012年04月18日 ⁄ 综合 ⁄ 共 2562字 ⁄ 字号 评论关闭

CVSListBox类的扩展使用

2013-5-2 flyfish  

www.boboding.com

 

一 在资源窗口中,选中资源右键-》资源包括-》输入如下代码

#ifndef_AFXDLL

#include"afxribbon.rc"

#endif

 

二 新建一个基于对话框的项目

将工具箱中的MFCVSListBox Control直接拖到窗口中

利用向导为该控件绑定一个变量

CVSListBox  m_List_ctlMain

向导自动添加头文件

#include"afxvslistbox.h"

三 增加输入规则

1 删除时,先弹出提示对话框,询问用户是否删除

2 在新建和编辑List中的内容时,检查是否存在重复内容

 

在微软提供的例子中重写关于按钮的消息

class CCustomEditListBox :public CVSListBox

{

    virtual void OnBrowse()

    {

        int nSel = GetSelItem();

        MessageBox(_T("Browse item..."));

        if (nSel == GetCount()) // New item

        {

            nSel = AddItem(_T("New text"));

            SelectItem(nSel);

        }

        else

        {

            SetItemText(nSel, _T("Updated text"));

        }

    }

};

 头文件和实现文件放在一个文件中

根据微软提供的例子按照这种写法进行仿写

类的继承关系

class CVSListBox :public CVSListBoxBase

class CVSListBoxBase :public CStatic

 

在CVSListBoxBase类中有

virtual BOOL OnBeforeRemoveItem(int/*iItem*/) { returnTRUE; }

 新建一个继承自CVSListBox的类CCustomEditListBox

 以下代码实现删除时,先弹出提示对话框,询问用户是否删除

 

class CCustomEditListBox:public CVSListBox

{

protected:

   BOOL OnBeforeRemoveItem(intnItem)

    {

        CString strText = GetItemText(nItem);       

            CString strPrompt=_T("");

            strPrompt.Format(_T("确定要删除【%s】吗?"),strText);

            if ( MessageBox(strPrompt,_T("提示"),MB_ICONQUESTION|MB_OKCANCEL)==IDOK)

            {

                    return TRUE;

            }  

        return FALSE;

    }

};

 

 

 

以下代码实现在新建和编辑List中的内容时,检查是否存在重复内容

 

 

class CCustomEditListBox :public CVSListBox

{

boolIsExist(CString strText)

     {

         for (int i=0;i<GetCount();i++)

         {

             CString strContent=GetItemText(i);

             if (strContent == strText )

             {

                 return true;//已经存在

             }

         }

         return false;

     }

 

     voidSetItemText(int iIndex, const CString& strText)

     {

         if (IsExist(strText))

         {

             CString strPrompt=_T("");

             strPrompt.Format(_T("【%s】已经存在"),strText);

             AfxMessageBox(strPrompt);

             EditItem(iIndex);

             m_wndEdit.SetWindowText(strText);

             m_wndEdit.SetSel(0,-1);//全选

            // m_wndEdit.SetSel(-1);//光标处于文字的末端

 

             return;

         }

         if (GetSafeHwnd() == NULL || m_pWndList ==NULL)

         {

             ASSERT(FALSE);

             return;

         }

 

         ASSERT_VALID(m_pWndList);

         m_pWndList->SetItemText(iIndex,0,strText);

             

     }

};

代码中使用CVSListBox类中的成员变量

CListCtrl* m_pWndList;

CVSListBoxEditCtrl  m_wndEdit;

还可以重写其他事件

virtualvoid OnAfterAddItem(int/*iItem*/) {}

virtualvoid OnAfterRenameItem(int/*iItem*/) {}

virtualvoid OnAfterMoveItemUp(int/*iItem*/) {}

virtualvoid OnAfterMoveItemDown(int/*iItem*/) {}

 

如需其他功能例如多列,输入掩码,只允许输入数字等等功能就需要对CListCtrl和CVSListBoxEditCtrl  进行扩展。

 

 

抱歉!评论已关闭.