Usage of insertitem and settitemtext functions in the clistctrl Control

Source: Internet
Author: User


When I first used the clistctrl control, I was not very familiar with the functions of insertitem and setrtitemtext. For example, if I didn't call the insertitem function first, I wrote the insertitemtext function later, does not work either. I checked msdn, and I didn't get any results. After some clicks, I suddenly realized. let's talk about the experience today, hoping that there will be less detours for common fans.

The function prototype of insertitem in msdn is as follows:
Int

Insertitem (
Const
Lvitem *

Pitem
);

Int insertitem (int

Nitem
, Lpctstr
Lpszitem

);

Int insertitem (int

Nitem
, Lpctstr
Lpszitem
,
Int

Nimage
);

Int
Insertitem (uint

Nmask
, Int

Nitem
, Lpctstr
Lpszitem
,
Uint

Nstate
, Uint

Nstatemask
, Int
Nimage
,
Lparam

Lparam
);


Generally, we use 2nd and the third types of items with a header.


To call the third object, you can declare a cimagelist object first.
M_imagelist.create (16, 16, true, 2, 2 );
M_imagelist.add (theapp. loadicon (idr_mainframe ));
M_listctrl.setimagelist (& m_imagelist, lvsil_small );
In this way, each row in the control has an application icon at the beginning.

Now let's take a look at the meaning of each insertitem parameter.
Nitem: the row index of the control.

Lpszitem
:
Control header name

........................................ ........................................ ........................................ ........................................ .........................................

Prototype of settitemtext Function
Bool
Setitemtext (

Int
Nitem
,
Int

Nsubitem
,
Lptstr
Lpsztext

);
* ****************** Pay attention to the parameters in this function

Nitem is nitem in insertitem,

That is to say, insertitem requests a row of data to the control sound. Remember that only the sound is called, then the work of Party data is implemented by setitemtext.
Example:

Clistctrl m_listctrl;

M_listctrl = new clistctrl ();


M_listctrl.creae (ws_child | ws_visible | ws_border | lvs_report | lvs_editlabels
, Bigrect, this, idc_list1

);

M_listctrl.insertitem (0, "01 ");

M_setitemtext (0, 1, "1 ");


M_setitemtext (0, 2, "2 ");
'''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''

As to how much data is to be added, it is up to the number of columns of your control.

Obtain the int columns =
M_listctr.getheaderctrl ()-> getitemcount ();

Obtain the int rows =
M_listctrl.getitemcount ();


The following is an example of my learning.
//
Dataview. cpp: Implementation of the cdataview class
//

# Include "stdafx. H"
# Include "data. H"

# Include "datadoc. H"
# Include "dataview. H"
# Include
"Editdlg. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF
This_file
Static char this_file [] = _ file __;
# Endif

//////////////////////////////////////// /////////////////////////////////////
//
Cdataview

Implement_dyncreate (cdataview, cview)

Begin_message_map (cdataview,
Cview)
// {Afx_msg_map (cdataview)
On_command (idm_user,
Onuser)
On_command (idm_up, onup)
On_command (idm_down,
Ondown)
On_command (idm_edit, onedit)
On_command (idm_add,
Onadd)
On_command (idm_delete, ondelete)
On_command (idm_save,
Onsave)
//} Afx_msg_map
// Standard printing
Commands
On_command (id_file_print,
Cview: onfileprint)
On_command (id_file_print_direct,
Cview: onfileprint)
On_command (id_file_print_preview,
Cview: onfileprintpreview)
On_policy (nm_click, idc_list1,
Onclicklist1)
On_policy (nm_dblclk, idc_list1,
Ondblclklist1)
On_policy (nm_killfocus, idc_list1,
Onkillfocuslist1)
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
//
Cdataview construction/destruction

Cdataview: cdataview ()
{
// Todo: Add construction code here

M_nindex =-1;
}

Cdataview ::~ Cdataview ()
{
}

Bool cdataview: precreatewindow (createstruct & CS)
{
// Todo:
Modify the window class or styles here by modifying
// The createstruct
CS

Return cview: precreatewindow (CS );
}

//////////////////////////////////////// /////////////////////////////////////
//
Cdataview drawing

Void cdataview: ondraw (CDC * PDC)
{
Cdatadoc * pdoc =
Getdocument ();
Assert_valid (pdoc );
// Todo: Add draw code for native
Data here
}

//////////////////////////////////////// /////////////////////////////////////
//
Cdataview Printing

Bool cdataview: onprepareprinting (cprintinfo * pinfo)
{
// Default
Preparation
Return doprepareprinting (pinfo );
}

Void cdataview: onbeginprinting (CDC */* PDC */, cprintinfo *
/* Pinfo */)
{
// Todo: add extra initialization before printing
}

Void cdataview: onendprinting (CDC */* PDC */, cprintinfo *
/* Pinfo */)
{
// Todo: Add cleanup after printing
}

//////////////////////////////////////// /////////////////////////////////////
//
Cdataview diagnostics

# Ifdef _ debug
Void cdataview: assertvalid ()
Const
{
Cview: assertvalid ();
}

Void cdataview: dump (cdumpcontext & DC)
Const
{
Cview: dump (DC );
}

Cdatadoc * cdataview: getdocument () // non-debug version is
Inline
{
Assert (m_pdocument-> iskindof (runtime_class (cdatadoc )));
Return
(Cdatadoc *) m_pdocument;
}
# Endif // _ debug

//////////////////////////////////////// /////////////////////////////////////
//
Cdataview message handlers

Void cdataview: oninitialupdate ()

{
Cview: oninitialupdate ();
 
// Todo: add your specialized code
Here and/or call the base class
CDC * Dc = getdc ();
Crect
Rect;
This-> getclientrect (& rect );
Cstring STR =
"Load database resources to the list ";
Csize SZ = Dc-> gettextextent (STR );
Rect. Right =
SZ. CX;
Rect. Bottom = Sz. Cy;
Cstatic * State = new
Cstatic;
State-> Create (STR, ws_child, rect, this );

State-> showwindow (sw_show );

This-> releasedc (DC );
 
Initlistctrl (rect );
M_imagelist.create (16, 16, true, 2, 2 );
M_imagelist.add (theapp. loadicon (idr_mainframe ));
 
}

Void cdataview: initlistctrl (crect rect)
{
Crect
Bigrect;
This-> getclientrect (& bigrect );
Bigrect. Top =
Rect. bottom;
M_listctrl.create (ws_child | ws_visible | ws_border | lvs_report | lvs_editlabels
, Bigrect, this, idc_list1 );
// M_listctrl.insert (0, "username ");
// M_listctrl.insertitem (1, "password ");
Cfont
* M_pfont = new cfont ();
M_pfont-> createfont (20 ,//
Nheight
0, // nwidth

0, // nescapement
0 ,//
Norientation
Fw_normal, // nweight

False, // bitalic
False ,//
Bunderline
0, // cstrikeout

Default_charset, // ncharset
Out_default_precis ,//
Noutprecision
Clip_default_precis, // nclipprecision

Default_quality, // nquality
Default_pitch | ff_swiss ,//
Npitchandfamily
"Arial ");
M_listctrl.setfont (m_pfont );

M_listctrl.setimagelist (& m_imagelist, lvsil_small );
M_listctrl.setextendedstyle (lvs_ex_fullrowselect | lvs_ex_gridlines );
M_listctrl.setbkcolor (RGB (247,247,255 ));

M_listctrl.settextcolor (RGB (0, 0, 255 ));

M_listctrl.settextbkcolor (RGB (247,247,255 ));
M_listctrl.insertcolumn (0, "ID", lvcfmt_left, 100,-1 );
M_listctrl.insertcolumn (1, "username", lvcfmt_left, 100,-1 );
M_listctrl.insertcolumn (2, "password", lvcfmt_left, 200,-1 );

M_listctrl.insertitem (0, "01 ");
M_listctrl.setitemtext (0, 1, "chetnao ");
M_listctrl.setitemtext (0, 2, "921984 ");


M_listctrl.insertitem (1, "02 ");
M_listctrl.setitemtext (1, 1, "Mada ");
M_listctrl.setitemtext (345678 ");

M_listctrl.insertitem (2, "03 ");
M_listctrl.setitemtext (2, 1, "luoxian ");
M_listctrl.setitemtext (2, 2, "987654 ");
}

Extern cdataapp theapp;
Void cdataview: onuser ()
{
// Todo: add
Your command handler code here
_ Recordsetptr
M_precordset;
Try
{
M_precordset =
Theapp. m_pconnection-> execute ("select * from
User12 ", null, ad1_text );
}
Catch (_ com_error
E)
{
Afxmessagebox (E. errormessage ());
Afxmessagebox (E. Description ());
Cstring
STR;
Str. Format ("% lD", E. Error );
Afxmessagebox (STR );
Return;
}
If (m_precordset-> BOF)
{
MessageBox ("no records in the table ");
Return;
}
Else
{
Int
Len =
M_listctrl.getheaderctrl ()-> getitemcount ();
While (! M_precordset-> adoeof)
{


M_listctrl.insertitem (2, (_ bstr_t) m_precordset-> getcollect ("ID "));

M_listctrl.insertitem (1, (_ bstr_t) m_precordset-> getcollect ("username "));

M_listctrl.insertitem (0, (_ bstr_t) m_precordset-> getcollect ("password "));
/* For (int
I = 0; I <Len; I ++)
{
M_listctrl.setitemtext (I, 0, (_ bstr_t) m_precordset-> getcollect ("ID "));
M_listctrl.setitemtext (I, 1, (_ bstr_t) m_precordset-> getcollect ("username "));
M_listctrl.setitemtext (I, 2, (_ bstr_t) m_precordset-> getcollect ("password "));
}*/

M_precordset-> movenext ();
}
}
}
Void
Cdataview: onclicklist1 (nmhdr * pnmhdr, lresult * presult)
{
Position Pos =
M_listctrl.getfirstselecteditemposition ();
M_nindex =
M_listctrl.getnextselecteditem (POS );

}
Void cdataview: ondblclklist1 (nmhdr * pnmhdr, lresult *
Presult)
{
Position Pos =
M_listctrl.getfirstselecteditemposition ();
M_nindex =
M_listctrl.getnextselecteditem (POS );
Ceditdlg DLG;
DLG. m_strno =
M_listctrl.getitemtext (m_nindex, 0 );
DLG. m_strname =
M_listctrl.getitemtext (m_nindex, 1 );
DLG. m_strpassword =
M_listctrl.getitemtext (m_nindex, 2 );

If (idok =
DLG. domodal ())
{
M_listctrl.setitemtext (m_nindex, 0, DLG. m_strno );

M_listctrl.setitemtext (m_nindex, 1, DLG. m_strname );
M_listctrl.setitemtext (m_nindex, 2, DLG. m_strpassword );
}
* Presult
= 0;

}

Void cdataview: onup ()
{
// Todo: add your command handler code
Here
M_listctrl.setfocus ();
If (m_nindex =
-1)
{
MessageBox ("select a row! ");
Return;
}
If (m_nindex =
0)
{
MessageBox ("the line you selected is the top line! ");
Return;
}
Cstring
Strno, strname, strpassword;
Strno =
M_listctrl.getitemtext (m_nindex, 0 );
Strname =
M_listctrl.getitemtext (m_nindex, 1 );
Strpassword =
M_listctrl.getitemtext (m_nindex, 2 );

M_listctrl.deleteitem (m_nindex );
M_listctrl.insertitem (m_nIndex-1, strno );
M_listctrl.setitemtext (m_nIndex-1, 1, strname );

M_listctrl.setitemtext (m_nIndex-1, 2, strpassword );

M_nindex --;
M_listctrl.setitemstate (m_nindex, lvis_focused | lvis_selected, lvis_focused | lvis_selected );

}
Void cdataview: onkillfocuslist1 (nmhdr * pnmhdr, lresult *
Presult)
{
M_nindex =-1;
}
Void cdataview: ondown ()
{
//
Todo: add your command handler code here
M_listctrl.setfocus ();

If (m_nindex =-1)
{
MessageBox ("select a row! ");
Return
;
}
If (m_nindex =
M_listctrl.getitemcount ()-1)
{
MessageBox ("this is the last line! ");
Return
;
}
Cstring strno, strname, strpassword;
Strno =
M_listctrl.getitemtext (m_nindex, 0 );
Strname =
M_listctrl.getitemtext (m_nindex, 1 );
Strpassword =
M_listctrl.getitemtext (m_nindex, 2 );

M_listctrl.deleteitem (m_nindex );
M_listctrl.insertitem (m_nindex + 1, strno );
M_listctrl.setitemtext (m_nindex + 1, 1, strname );

M_listctrl.setitemtext (m_nindex + 1, 2, strpassword );
M_nindex ++;

M_listctrl.setitemstate (m_nindex, lvis_focused | lvis_selected, lvis_focused | lvis_selected );
}

Void cdataview: onedit ()
{
// Todo: add your command handler code
Here
Position Pos = m_listctrl.getfirstselecteditemposition ();

M_nindex = m_listctrl.getnextselecteditem (POS );
If (m_nindex =
-1)
{
MessageBox ("select a row! ");
Return;
}
Ceditdlg
DLG;
DLG. m_strno = m_listctrl.getitemtext (m_nindex, 0 );
DLG. m_strname =
M_listctrl.getitemtext (m_nindex, 1 );
DLG. m_strpassword =
M_listctrl.getitemtext (m_nindex, 2 );

If (idok =
DLG. domodal ())
{
M_listctrl.setredraw (false );
M_listctrl.deleteitem (m_nindex );

M_listctrl.insertitem (m_nindex, DLG. m_strno );

M_listctrl.setitemtext (m_nindex, 1, DLG. m_strname );
M_listctrl.setitemtext (m_nindex, 2, DLG. m_strpassword );
M_listctrl.setredraw (true );
}
}

Void cdataview: onadd ()
{
// Todo: add your command handler code
Here
Ceditdlg DLG;
If (DLG. domodal () = idok)
{
Int
Count = m_listctrl.insertitem (m_listctrl.getitemcount (), DLG. m_strno );
M_listctrl.setitemtext (count, 1, DLG. m_strname );

M_listctrl.setitemtext (count, 2, DLG. m_strpassword );
}
}

Void cdataview: ondelete ()
{
// Todo: add your command handler code
Here
If (m_nindex =-1)
{
MessageBox ("select a row! ");
Return
;
}
M_listctrl.deleteitem (m_nindex );
Uint flag =
Lvis_selected | lvis_focused;
M_listctrl.setitemstate (m_nIndex-1, flag,
Flag );
}

Void cdataview: onsave ()
{
// Todo: add your command handler code
Here
Cfile
F;
If (F. Open ("save.txt", cfile: modecreate | cfile: modewrite, null ))
{
Cstring
STR = "ID/T/tusername/T/tpassword/R/N ";
F. Write (STR, strlen (STR) + 1 );
For (int
I = 0; I <m_listctrl.getitemcount (); I ++)
{
STR = m_listctrl.getitemtext (I, 0) + "/T" + m_listctrl.getitemtext (I, 1) +
"/T" + m_listctrl.getitemtext (I, 2) + "/R/N ";

F. Write (STR, strlen (STR) + 1 );
}
F. Close ();
MessageBox ("saved successfully! ");
}
Else
{
MessageBox ("Saving failed! ");
}
 
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.