Use the listcontrol (Summary 2) to edit the ListCon list box.

Source: Internet
Author: User
Tags setcell
This article comprehensively introduces how to edit any sub-item in ListControl and introduces a little more content. It doesn't mean much to translate it. Generally, a VC programmer will often encounter ListControl, and there are many ways to display data in ListControl, the default edit function is not available in ListControl. Therefore, in this smart essay, the cow person will teach you how

This article comprehensively introduces how to edit any sub-item in List Control and introduces a little more content. It doesn't make much sense to translate it, which means that a VC programmer will often encounter List Control, list Control has many ways to display data. By default, List Control does not have the editing function. Therefore, in this short article, the cool-man will teach you how

This topic describes how to edit any subitem in List Control.

Introduction

There is a lot of content, and it doesn't mean much to translate it. Basically, a VC programmer will often encounter List Control. List Control has many ways to display data, but the default function is not available in List Control, therefore, in this smart text, the cow man taught you how to implement this function. This article is based on vc mfc. You can also look at it with other tools:

Void CMultipleColumnsDlg: OK ()

{

CDialog: EndDialog (0); // Add this line

}

Void CMultipleColumnsDlg: OnExit ()

{

CDialog: EndDialog (0); // Add this line

}

Next, add a ListCtrl control. Remember to set the style of ListCtrl to Report. This is to implement the function of displaying multiple rows.

ThenAdd a text Box Edit Box to remove its Border style attribute

Added an InsertItems () member function to write the display content of ListControl.

Void CMultipleColumnsDlg: InsertItems ()

{

HWND hWnd =: GetDlgItem (m_hWnd, IDC_LIST1 );

// Set the LVCOLUMN structure with the required

// Column information

LVCOLUMN list;

List. mask = LVCF_TEXT | LVCF_WIDTH |

LVCF_FMT | LVCF_SUBITEM;

List. fmt = LVCFMT_LEFT;

List. cx = 50;

List. pszText = "S. No ";

List. iSubItem = 0;

// Inserts the column

: SendMessage (hWnd, LVM_INSERTCOLUMN,

(WPARAM) 0, (WPARAM) & list );

List. cx = 100;

List. pszText = "Name ";

List. iSubItem = 1;

: SendMessage (hWnd, LVM_INSERTCOLUMN,

(WPARAM) 1, (WPARAM) & list );

List. cx = 100;

List. pszText = "Address ";

List. iSubItem = 2;

: SendMessage (hWnd, LVM_INSERTCOLUMN,

(WPARAM) 1, (WPARAM) & list );

List. cx = 100;

List. pszText = "Country ";

List. iSubItem = 2;

: SendMessage (hWnd, LVM_INSERTCOLUMN,

(WPARAM) 1, (WPARAM) & list );

// Inserts first Row with four columns insert Row

SetCell (hWnd, "1", 0, 0 );

SetCell (hWnd, "Prabhakar", 0, 1 );

SetCell (hWnd, "Hyderabad", 0, 2 );

SetCell (hWnd, "India", 0, 3 );

// Inserts second Row with four columns.

SetCell (hWnd, "2", 1, 0 );

SetCell (hWnd, "Uday", 1, 1 );

SetCell (hWnd, "Chennai", 1, 2 );

SetCell (hWnd, "India", 1, 3 );

}

The custom SetCell () function is used to insert data.

Void CMultipleColumnsDlg: SetCell (HWND hWnd1,

CString value, int nRow, int nCol)

{

TCHAR SzString [256];

Wsprintf (szString, value, 0 );

// Fill the LVITEM structure with

// Values given as parameters.

LVITEM lvItem;

LvItem. mask = LVIF_TEXT;

LvItem. iItem = nRow;

LvItem. pszText = szString;

LvItem. iSubItem = nCol;

If (nCol> 0)

// Set the value of listItem

: SendMessage (hWnd1, LVM_SETITEM,

(WPARAM) 0, (WPARAM) & lvItem );

Else

// Insert the value into List

ListView_InsertItem (hWnd1, & lvItem );

}

// Obtain data in the project through rows and columns

CString CMultipleColumnsDlg: GetItemText (

HWND hWnd, int nItem, int nSubItem) const

{

LVITEM lvi;

Memset (& lvi, 0, sizeof (LVITEM ));

Lvi. iSubItem = nSubItem;

CString str;

Int nlen= 128;

Int nRes;

Do

{

NLen * = 2;

Lvi. cchTextMax = nLen;

Lvi. pszText = str. GetBufferSetLength (nLen );

NRes = (int): SendMessage (hWnd,

LVM_GETITEMTEXT, (WPARAM) nItem,

(LPARAM) & lvi );

Str. ReleaseBuffer ();

Return str;

}

}

// Add two member variables for the window class:

Int nItem, nSubItem;

Use Class wizard to add the NM_CLICK response. When a user clicks any position, an Edit Box is displayed, and the data can be modified.

Void CMultipleColumnsDlg: OnClickList (

NMHDR * pNMHDR, LRESULT * pResult)

{

Invalidate ();

HWND hWnd1 =: GetDlgItem (m_hWnd, IDC_LIST1 );

LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;

RECT rect;

// Get the row number

NItem = temp-> iItem;

// Get the column number

NSubItem = temp-> iSubItem;

If (nSubItem = 0 | nSubItem =-1 | nItem =-1)

Return;

// Retrieve the text of the selected subItem

// From the list

CString str = GetItemText (hWnd1, nItem,

NSubItem );

RECT rect1, rect2;

// This macro is used to retrieve the Rectanle

// Of the selected SubItem

ListView_GetSubItemRect (hWnd1, temp-> iItem,

Temp-> iSubItem, LVIR_BOUNDS, & rect );

// Get the Rectange of the listControl

: GetWindowRect (temp-> hdr. hwndFrom, & rect1 );

// Get the Rectange of the Dialog

: GetWindowRect (m_hWnd, & rect2 );

Int x = rect1.left-rect2.left;

Int y = rect1.top-rect2.top;

If (nItem! =-1)

: SetWindowPos (: GetDlgItem (m_hWnd, IDC_EDIT1 ),

HWND_TOP, rect. left + x, rect. top + 4,

Right-rect.left-3,

Rect. bottom-rect.top-1, NULL );

: ShowWindow (: GetDlgItem (m_hWnd, IDC_EDIT1), SW_SHOW );

: SetFocus (: GetDlgItem (m_hWnd, IDC_EDIT1 ));

// Draw a Rectangle around the SubItem

: Rectangle (: GetDC (temp-> hdr. hwndFrom ),

Rect. left, rect. top-1, rect. right, rect. bottom );

// Set the listItem text in the EditBox

: SetWindowText (: GetDlgItem (m_hWnd, IDC_EDIT1), str );

* PResult = 0;

}

To handle the ENTER key we need to write the virtual function OnOk (response To ENTER key)

Afx_msg void OnOK ();

In MultipleColumnsDlg. cpp write the following code.

// This function handles the ENTER key

Void CMultipleColumnsDlg: OnOK ()

{

CWnd * pwndCtrl = GetFocus ();

// Get the control ID which is

// Presently having the focus

Int ctrl_ID = pwndCtrl-> GetDlgCtrlID ();

CString str;

Switch (ctrl_ID)

{ // If the control is the EditBox

Case IDC_EDIT1:

// Get the text from the EditBox

GetDlgItemText (IDC_EDIT1, str );

// Set the value in the listContorl with

// Specified Item & SubItem values

SetCell (: GetDlgItem (m_hWnd, IDC_LIST1 ),

Str, nItem, nSubItem );

: SendDlgItemMessage (m_hWnd, IDC_EDIT1,

WM_KILLFOCUS, 0, 0 );

: ShowWindow (: GetDlgItem (m_hWnd, IDC_EDIT1 ),

SW_HIDE );

Break;

Default:

Break;

}

}

The last step is to set the List Control style in OnInitDialog.

ListView_SetExtendedListViewStyle(: GetDlgItem

(M_hWnd, IDC_LIST1), LVS_EX_FULLROWSELECT |

LVS_EX_GRIDLINES );

InsertItems ();

: ShowWindow (: GetDlgItem (m_hWnd, IDC_EDIT1), SW_HIDE );

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.