Edit List control data in MFC (edit lines, copy & paste)

2.8k views Asked by At

I have a list control with some data, and i need to be able to edit column (i have few columns but only one of them should be editable), also i need to be able in some way to copy multiple rows from this column and also put data (paste) from clipboard. Is this possible to enable that features with minimum efforts? Thank you.

Update: I found solution for editing filed, but it works strange. Here's the article http://www.codeproject.com/Articles/1124/Editing-Sub-Items-in-List-Control

With authors example it works pretty good, but when i tried to remake it for my tabbed project i got an incorrect display of editbox, it's related to tabbed dialog coordinates but i still can't figure out how to fix it.

enter image description here

1

There are 1 answers

1
rrirower On BEST ANSWER

The article you reference has some problems. If you take a look at the discussions posts after the article, you’ll notice some comments indicating a problem with the placement of the CEdit control. In particular, look for "CEdit placement error". More importantly, if you take a look at the code that was posted, you'll see hard coded adjustments to the SetWindowPos command. It’s never a good idea to hard code adjustments. They should always be calculated dynamically if possible.

I have succeeded in fixing the positioning problems by adding one line of code and removing the hard coded adjustments. See my code below.

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);
::MapWindowPoints(hWnd1, m_hWnd, reinterpret_cast<LPPOINT>(&rect), 2);

//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, rect.top,
    rect.right - rect.left,
    rect.bottom - rect.top, 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, rect.right, rect.bottom);
//Set the listItem text in the EditBox
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT1), str);

The line I added is for the MapWindowPoints to convert the coordinates of the list control item to the coordinate space of the dialog. I've also commented out drawing the rectangle around the edit box since it doesn't seem to add any value.