listbox java_CVSListBox类的扩展使用

CVSListBox类的扩展使用

2013-5-2 flyfish

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

#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

{

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  进行扩展。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值