Extension of the cvslistbox class
2013-5-2 flyfish
Www.boboding.com
1. In the resource window, right-click the resource and enter the following code:
# Ifndef_afxdll
# Include "afxribbon. RC"
# Endif
2. Create a dialog-based project
Drag the mfcvslistbox control in the toolbox to the window.
Use the Wizard to bind a variable to the control.
Cvslistbox m_list_ctlmain
Wizard automatically adds header files
# Include "afxvslistbox. H"
3. Add input rules
1. When deleting a file, a prompt dialog box is displayed, asking whether to delete the file.
2. When creating and editing the content in the list, check whether duplicate content exists.
In the example provided by Microsoft, rewrite the message about the button
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 "));
}
}
};
Put the header file and implementation file in one file
Perform copywriting based on the example provided by Microsoft.
Class inheritance relationship
Class cvslistbox: Public cvslistboxbase
Class cvslistboxbase: Public cstatic
In the cvslistboxbase class
Virtual bool onbeforeremoveitem (INT/* iItem */) {returntrue ;}
Create a class ccustomeditlistbox inherited from cvslistbox
When the following code is used to delete a file, a prompt dialog box is displayed, asking whether to delete the file.
Class ccustomeditlistbox: Public cvslistbox
{
Protected:
Bool onbeforeremoveitem (intnitem)
{
Cstring strtext = getitemtext (nitem );
Cstring strprompt = _ T ("");
Strprompt. Format (_ T ("are you sure you want to delete [% s? "), Strtext );
If (MessageBox (strprompt, _ T ("prompt"), mb_iconquestion | mb_okcancel) = idok)
{
Return true;
}
Return false;
}
};
The following code checks whether duplicate content exists when creating and editing the content in the list.
Class ccustomeditlistbox: Public cvslistbox
{
Boolisexist (cstring strtext)
{
For (INT I = 0; I <getcount (); I ++)
{
Cstring strcontent = getitemtext (I );
If (strcontent = strtext)
{
Return true; // already exists
}
}
Return false;
}
Voidsetitemtext (INT iindex, const cstring & strtext)
{
If (isexist (strtext ))
{
Cstring strprompt = _ T ("");
Strprompt. Format (_ T ("[% s] already exists"), strtext );
Afxmessagebox (strprompt );
Edititem (iindex );
M_wndedit.setwindowtext (strtext );
M_wndedit.setsel (0,-1); // select all
// M_wndedit.setsel (-1); // The cursor is at the end of the text
Return;
}
If (getsafehwnd () = NULL | m_pwndlist = NULL)
{
Assert (false );
Return;
}
Assert_valid (m_pwndlist );
M_pwndlist-> setitemtext (iindex, 0, strtext );
}
};
Use member variables in the cvslistbox class in the code
Clistctrl * m_pwndlist;
Cvslistboxeditctrl m_wndedit;
You can also rewrite other events.
Virtualvoid onafteradditem (INT/* iItem */){}
Virtualvoid onafterrenameitem (INT/* iItem */){}
Virtualvoid onaftermoveitemup (INT/* iItem */){}
Virtualvoid onaftermoveitemdown (INT/* iItem */){}
If you need other functions, such as multiple columns, input mask, and only numbers, you need to expand clistctrl and cvslistboxeditctrl.