Make clistctrl editable)

Source: Internet
Author: User

Clistctrl is a frequently used control that is usually used to display the database content. However, clistctrl is not easy to use. We certainly hope that clistctrl can be as easy to use as Excel, so we have to make improvements based on the original.

1. Enable clistctrl to edit subitem

1. Add a clistctrl control in the dialog box and add the clistctrl variable m_list.

2. initialize the control

Set the control style (copy online)

Long lstyle;
Lstyle = getwindowlong (m_list.m_hwnd, gwl_style); // get the style of the current window
Lstyle | = lvs_report; // set the style
Setwindowlong (m_list.m_hwnd, gwl_style, lstyle); // set the style
DWORD dwstyle = m_list.getextendedstyle ();
Dwstyle | = lvs_ex_fullrowselect; // select an exercise to highlight the entire line (only applicable to listctrl in the report style)
Dwstyle | = lvs_ex_gridlines; // gridline (only applicable to listctrl with report style)
M_list.setextendedstyle (dwstyle); // you can specify the extended style.

Insert column

M_list.insertcolumn (0, "orderid", lvcfmt_left, 80 );
M_list.insertcolumn (1, "customerid", lvcfmt_left, 80 );
M_list.insertcolumn (2, "orderdate", lvcfmt_left, 80 );
M_list.insertcolumn (3, "feight", lvcfmt_left, 80 );
M_list.insertcolumn (4, "shipname", lvcfmt_left, 80 );
M_list.insertcolumn (5, "shipaddress", lvcfmt_left, 80 );
M_list.insertcolumn (6, "shipcountry", lvcfmt_left, 80 );

Insert row

M_list.insertitem (I, sorderid); // sorderid is the inserted data (a variable of the lpctstr type). I indicates the position of the data in clistctrl (an int type variable ).
M_list.setitemtext (I, 1, scustomerid );
M_list.setitemtext (I, 2, sorderdate );
M_list.setitemtext (I, 3, sfeight );
M_list.setitemtext (I, 4, sshipname );
M_list.setitemtext (I, 5, sshipaddress );
M_list.setitemtext (I, 6, sshipcountry );

3. Edit clistctrl

The method is as follows: When you click the clistctrl control, highlight one line. When you double-click the clistctrl control, move a cedit control to the position of the column you double-click (the content of the column you clicked is displayed in the cedit control. And the cedit control gets the focus. Then, you can modify the content in cedit. When your mouse clicks somewhere else, cedit will lose its focus. At the same time, we update the content in cedit to clistctrl, thus modifying the subitem of clistctrl.

First, add a cedit control in the dialog box and define the cedit variable m_edit. This control is hidden in the initialization function of the dialog box.
M_edit.showwindow (sw_hide );

Then add the message nm_dblclk to clistctrl (double-click the event) and add code to the message.

Nm_listview * pnmlistview = (nm_listview *) pnmhdr;
Crect RC;
If (pnmlistview-> iItem! =-1)
{
M_row = pnmlistview-> iItem; // m_row indicates the row sequence number of the selected row (INT type member variable)
M_column = pnmlistview-> isubitem; // m_column is the column number of the selected row (INT type member variable)
M_list.getsubitemrect (pnmlistview-> iItem, pnmlistview-> isubitem, lvir_label, RC); // obtain the rectangle of the subitem
RC. Left + = 3;
RC. Top + = 2;
RC. Right + = 3;
RC. Bottom + = 2;

Char * Ch = new char [128];
M_list.getitemtext (pnmlistview-> iItem, pnmlistview-> isubitem, CH, 128); // obtain the subitem content
M_edit.setwindowtext (CH); // display the subitem content in the edit box.
M_edit.showwindow (sw_show); // The edit box is displayed.
M_edit.movewindow (& rc); // move the edit box to the subitem and overwrite it to the subitem.
M_edit.setfocus (); // make the edit box focus
M_edit.createsolidcaret (1, RC. Height ()-5); // create a cursor
M_edit.showcaret (); // displays the cursor
M_edit.setsel (-1); // move the cursor to the end
}
* Presult = 0;

Then, add the message en_killfocus when cedit loses focus. Add code to the message to update the content in the edit box to clistctrl.

Cstring STR;
M_edit.getwindowtext (STR); // get the content of the edit box.
M_list.setitemtext (m_row, m_column, STR); // update the content to clistctrl.
M_edit.showwindow (sw_hide); // hide the edit box.

After the preceding steps, you can edit the subitem that implements clistctrl. Of course, this is incomplete. What to do next: 1. directly derive an extension class clistctrlex from clistctrl. 2. Add self-painting-related code. 3. Implement the virutal function. 4. Continue to add and double-click the subitem to bring up a ccombobox control function. 5. You can adjust the Row Height. 6. Click the row header to sort the data. And so on

Some good stuff written by others

[1] http://www.codeproject.com/KB/list/virtuallist.aspx

[2] http://www.codeproject.com/KB/list/quicklist.aspx#text

[3] http://www.codeproject.com/KB/miscctrl/gridctrl.aspx#EditValidate

[4] http://www.chinaitpower.com/A/2004-02-14/132385.html

【5】 http://m.cnblogs.com/35075/1556904.html

[6] http://www.vckbase.com/document/viewdoc? Id = 1853

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.