Delete columns in the List View in an MFC single-document application

Source: Internet
Author: User

1. List View Controls in the dialog box

The List View Control of the grid report type is often used when writing some database programs. This control is not much different from other controls for the dialog box program. Set the view attribute to the report type, associate it with a member variable of the clistctrl control type, and set its extension type to the grid type in the initialization dialog box function, and add columns for it.

However, for a program with certain flexibility, the control columns in the original list view are not static. We usually encapsulate it into different functions, call different layout functions under different commands, and then present data in different list views. For example, when you click "patient information", the List View displays data in the column of patient information. When you click "medical information, the List View displays the latest Diagnosis and Treatment Information in a column.

The above two operations are completed in a program interface. This leads to a problem: What should we do when we switch the layout in the List View? View the clistctrl member in msdn. You can find the clistctrl: deletecolumn () member function. In the example below this function, msdn provides a code to delete all columns. The Code is as follows.

int nColumnCount = m_myListCtrl.GetHeaderCtrl()->GetItemCount();// Delete all of the columns.for (int i=0; i < nColumnCount; i++){   m_myListCtrl.DeleteColumn(0);}

This code is obvious. first obtain the number of columns in the List View, and then delete each column cyclically. Before deleting a column, call the deleteallitems () function to delete all elements in the List View.

2. List View in a single document


The List View control can be used in the dialog box program, and can also be used in the document program. You only need to change the base class of the View class to cformview at the last step of creating the program. However, we usually use clistview as the base class to use the List View Single-document program. In this way, we can use the View window as a list view.

After creating the program using the wizard. We can initialize the View window in the cxxxview: oninitialupdate () function, insert columns, and set some extension types. The general code is as follows.

Clistctrl & listctrl = getlistctrl (); listctrl. modifystyle (0, lvs_report); listctrl. setextendedstyle (lvs_ex_gridlines | lvs_ex_fullrowselect); // set the extension type to grid and all-selected listctrl. insertcolumn (0, _ T ("name"), lvcfmt_center, 100); listctrl. insertcolumn (1, _ T ("gender"), lvcfmt_center, 100 );

 

3. Delete columns in the list view in a single document Program

This is the focus of my article. If you want to create a program, this program has several buttons in the toolbar. If you click different buttons, the layout in the list view will change and the corresponding data will be listed. First, you have to encapsulate different la s into different functions. Then, you can call different la s in different button message response functions. As described in the first article, you must first Delete the original column. Of course, you find the clistctrl: deletecolumn () method in msdn and see the example provided below. However, if you use that piece of code without modification, your program will get a memory that cannot be read.

The reason is that a problem occurs when getitemcount () is called. This means that the problem lies in the number of columns in the List View. Isn't the number of columns in the list view not available in the document program? Of course not. You can use the: sendmessage () function to send the lvm_getitemcount message to get the number of columns. The Code is as follows.

Clistctrl & m_mylistctrl = getlistctrl (); hwnd hlist = listview_getheader (m_mylistctrl); // use this macro to obtain the view header handle int ncolumncount =: sendmessage (hlist, hdm_getitemcount, 0, 0 );

If the number of columns in the view is ncolumncount, call the For Loop in section 1st to delete the column. Before deleting a column, call the deleteallitems () function to delete all elements in the List View.

However, you can delete a column even if the number of columns is not obtained. You can use the deletecolumn () method to return the value feature and use a while statement to delete all columns. If this function is successfully called, no value is returned. If the call fails, no value is returned. This means that if a Column exists, non-0 is returned, and 0 is returned if the deletion is complete. The Code is as follows. The clearlist () function is a function written by the author to delete columns in the document view list.

void CXXXView::ClearList(){CListCtrl& m_myListCtrl = GetListCtrl();m_myListCtrl.DeleteAllItems();// Delete all of the columns.while (m_myListCtrl.DeleteColumn(0));}

 

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.