How to edit cell in listcontrol mfc? - c++

I need edit my text in second column and I don't want use MFC grid control.
how I can edit cell by kicking on it.
please give me simple example.
There what I have:
void CTab1::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, m_LISTC);
}
BEGIN_MESSAGE_MAP(CTab1, CDialogEx)
ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LIST1, OnEndEdit)
END_MESSAGE_MAP()
// CTab1 message handlers
BOOL CTab1::OnInitDialog()
{
CDialogEx::OnInitDialog();
m_LISTC.InsertColumn(0, L"Buttons", LVCFMT_LEFT,50);
m_LISTC.InsertColumn(1, L"Time", LVCFMT_LEFT, 50);
m_LISTC.InsertColumn(2, L"State", LVCFMT_LEFT, 40);
for (int i = 0; i < 12; ++i)
{
CString F;
F.Format(L"F%d", i + 1);
m_LISTC.InsertItem(m_LISTC.GetItemCount(), F, -1);
int row = 1;
int col = 10;
m_LISTC.SetItemState(row, col, m_LISTC.GetItemState(row, col) | LVN_ENDLABELEDIT);
}
return TRUE;
}
void CTab1::OnEndEdit(NMHDR* pNMHDR, LRESULT* pResult)
{
NMLVDISPINFO* pLVDI = reinterpret_cast< NMLVDISPINFO* >(pNMHDR);
if (pLVDI->item.pszText)
m_LISTC.SetItemText(1, 0, pLVDI->item.pszText);
*pResult = 0;
}
but it not working. so I still can edit cell's of second column.

Unfortunately it isn't possible to utilize LVS_EDITLABELS and LVN_ENDLABELEDIT for editing other columns than the first.
I just must create edit control on that cell...

Related

C++ update mfc ui on change of data

I'm programming an mfc dialog. The OnInitDialog method fills my CListCtrl and starts a timer:
BOOL MyDialog::OnInitDialog()
{
mListCtrl.InsertColumn(0, _T("Signal"), LVCFMT_LEFT, 90);
mListCtrl.InsertColumn(1, _T("Result"), LVCFMT_LEFT, 90);
MyList* myList = GetList();
for (unsigned int i = 0; i < myList->Size(); i++) {
MyObject* o = myList->operator[](i);
int nIndex = mResultList.InsertItem(0, _T(o->GetName()));
mListCtrl.SetItemText(nIndex, 1, _T(o->GetResult()));
}
m_nTimer = SetTimer(1234, 200, NULL);
return true;
}
This works so far...
In the OnTimer(UINT_PTR nIDEVENT) I call UpdateData(false);
void MyDialog::OnTimer(UINT_PTR nIDEvent)
{
UpdateData(false);
}
I need this timer, because a background thread is modifying the list returned by GetList() and I want to display changes immediately. (I was hoping this works somehow like databinding in WWPF).
But the refresh of my view only happens when the dialog is re-initialized.
I guess this is because InsertItem() takes a LPCTSTR and not a char*?
Can anybody explain a way how to fix this issue please?
Thanks in advance.

Spin Control GetPos() value is delayed

I have an edit control buddied with a spin control with the initial position set to 0. When the up arrow is clicked the edit box goes from 0 to 1, which is fine. But when I use GetPos() the MyValue is 0. When the spin control is incremented again from 1 to 2, the MyValue becomes 1. When down arrow is pressed the edit box goes from 2 to 1, but the value becomes 2. It seems that the MyValue is always one action behind the spin control.
BOOL CAlphaDlg::OnInitDialog()
{
// default code left out to keep it short ...
// TODO: Add extra initialization here
// set range and initial position
mSpinControl.SetRange(0, 3600); // range
mSpinControl.SetPos(0); // inital position
MyValue = mSpinControl.GetPos();
// display initial value in buddy editcontrol
mEditControlDisplay.Format("%d", MyValue);
UpdateData(false);
return TRUE;
}
void CAlphaDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
UpdateData(true);
int MyValue= mSpinControl.GetPos();
std::cout << MyValue << std::endl;
*pResult = 0;
}
I have tried getting the value from the editcontrol but the value is exhibiting the same behavior. How do I get the value of the GetPos() to match what is showing in the edit control?
Thank you in advance.
edit: Here is the full code
// AlphaDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Alpha.h"
#include "AlphaDlg.h"
#include "afxdialogex.h"
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CAlphaDlg dialog
CAlphaDlg::CAlphaDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_ALPHA_DIALOG, pParent)
, mEditControlDisplay(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAlphaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, mEditControlDisplay);
DDX_Control(pDX, IDC_SPIN1, mSpinControl);
}
BEGIN_MESSAGE_MAP(CAlphaDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN1, &CAlphaDlg::OnDeltaposSpin1)
END_MESSAGE_MAP()
// CAlphaDlg message handlers
BOOL CAlphaDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
mSpinControl.SetRange(0, 10);
mSpinControl.SetPos(0);
int MyValue = mSpinControl.GetPos();
mEditControlDisplay.Format("%d", MyValue);
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CAlphaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CAlphaDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAlphaDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CAlphaDlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
UpdateData(true);
int MyValue = mSpinControl.GetPos();
std::cout << mEditControlDisplay << std::endl;
std::cout << MyValue << std::endl;
}
Firstly, consider mapping your edit box to a int and then you do not need to cast to a string. And the default value is 0 to begin with so the spinner will be OK. You can also switch off the auto buddy.
In the deltapos handler you could do this:
void CMFCApplication1Dlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
SetDlgItemInt(IDC_EDIT1, pNMUpDown->iPos);
UpdateData(TRUE);
CString a;
a.Format(_T("%d"), iNumberValue);
AfxMessageBox(a);
}
For me the results in the popup message and the edit control are the same.
Update
You can workout what the new value would be by looking at the structure:
void CMFCApplication1Dlg::OnDeltaposSpin1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
int iCurrentPos = pNMUpDown->iPos;
if (pNMUpDown->iDelta > 0)
iCurrentPos++;
else
iCurrentPos--;
CString strNewValue;
strNewValue.Format(_T("%d"), iCurrentPos);
AfxMessageBox(strNewValue);
*pResult = 0;
}
For Consideration
You might want to give some thought to CSpinButtonCtrl::SetAccel which sets the acceleration for a spin button control.
In your case I don't think it is going to matter because your range is only 10
units. But if you had a larger range then it is possible that it would increment by more than one unit. It is just something to keep in mind.
According to this reference for UDN_DELTAPOS it says:
The iDelta member of the structure is a signed integer that contains the proposed change in position.
So you could improve the code and increment/decrement based on the iDelta value instead of 1. This would factor in for acceleration. So:
int iCurrentPos = pNMUpDown->iPos + pNMUpDown->iDelta;

CListControl Insert Column Debug Assertion Failure

In my application, I create a modal dialog that contains a mfc List Control. When I don't initialize any columns or items in the List Control, the dialog shows without error. When I try to add a column to the List Control, I get the following Debug Assertion Failed message:
If it helps, the breakpoint is at
_AFXCMN_INLINE int CListCtrl::InsertColumn(int nCol, const LVCOLUMN* pColumn)
{ ASSERT(::IsWindow(m_hWnd)); return (int) ::SendMessage(m_hWnd, LVM_INSERTCOLUMN, nCol, (LPARAM)pColumn); }
I am trying to add the column headers with the following code in OnInitDialog():
BOOL EventL::OnInitDialog()
{
m_ListEventLog.InsertColumn(0, _T("Description"), LVCFMT_LEFT, 250); //Failure happens HERE
//m_ListEventLog.InsertColumn(0, "Description", LVCFMT_LEFT, 200, 0); //I have also tried things such as this.
return FALSE;
}
I add column headers to other CListControls in my application in this way, without problems. The modal dialog is called with the code:
void ListOption::OnBnClickedEventLog()
{
EventL eventLog;
eventLog.DoModal();
}
Maybe you forgot to call the default function:
BOOL EventL::OnInitDialog()
{
BOOL res = CDialog::OnInitDialog();
m_ListEventLog.InsertColumn(0, _T("Description"), LVCFMT_LEFT, 250); //Failure happens HERE
//m_ListEventLog.InsertColumn(0, "Description", LVCFMT_LEFT, 200, 0); //I have also tried things such as this.
return res; // or return FALSE;
}
That's why ASSERT(::IsWindow(m_hWnd)) fails, because m_hWnd of ListView control is not ready. m_hWnd of dialog would not be ready either.
I had the same issue, until i added DDX_Control(pDX, IDC_LIST1, movies); to
void MainDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST1, movies);
}
movies - is name of the Listcontrol
CListCtrl movies;

DDX_Control example for mfc

I am not able to get the DDX_Control working example.
When I create the dialog box, I am not able to create a reference for the control object.
Google doesn't have examples as well.
Thanks.
void CEditDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_COMBO1, m_cmbBox);
DDX_Text(pDX, IDC_EDIT1, m_Edit);
}
void CMFCApplicationDDEView::OnActionEdit2()
{
// TODO: Add your command handler code here
CEditDialog dlg;
CString str;
dlg.m_cmbBox.GetLBText(0, str);
if (dlg.DoModal() == IDOK)
{
MessageBox(dlg.cmbItemStr);
}
}
dlg.m_cmbBox is NULL. Why is it null and how can I reference it in my view
#barmak is correct in saying that you cannot access dialog box controls directly before the InitDialog() has executed.
However, you can set / retrieve the text of the edit portion of a combo box by using DDX_CBString, like:
// in .h file
CString m_cmbItemStr;
// in .cpp
void CEditDialog::DoDataExchange(CDataExchange* pDX)
{ CDialog::DoDataExchange(pDX);
DDX_CBString(pDX, IDC_COMBO1, m_cmbItemStr);
DDX_Text(pDX, IDC_EDIT1, m_Edit);
}
void CMFCApplicationDDEView::OnActionEdit2()
{ CEditDialog dlg;
CString str = TEXT("some value");
dlg.m_cmbItemStr = str;
if (dlg.DoModal() == IDOK)
MessageBox(dlg.m_cmbItemStr);
}
Your code for combo box and dialog box is correct but m_cmbBox.GetLBText() cannot be used before and after DoModal() because there is no window handle. Override like code below, then access combo_str instead of accessing windows
BEGIN_MESSAGE_MAP(CEditDialog, CDialog)
ON_COMMAND(IDOK, OnOK)
//...
END_MESSAGE_MAP()
BOOL CEditDialog::OnInitDialog()
{
BOOL res = CDialog::OnInitDialog();
//Dialog is created, window handles are available, set text here
return res;
}
void CEditDialog::OnOK()
{
//get text before dialog's window handles are destroyed
int sel = m_cmbBox.GetCurSel();
if (sel >= 0) m_cmbBox.GetLBText(sel, cmbItemStr);
CDialog::OnOK();
}

Get row height of a CListCtrl in MFC

I want to resize my CListCtrl as Report for displaying only n rows with text or empty. And I need the height of a row (they all are of the same size and constant).
CRect rect;
myList.GetWindowRect(&rect);
ScreenToClient(&rect);
myList.MoveWindow(rect.left, rect.top, rect.Width(), 16*8-5 /* TODO */);
CListCtrl with Variable Row Height
/*
1a. Setup a typical CListCtrl with owner draw
1b. Fill the CListCtrl with the text you want, as you would normally
2. Setup a CListBox with OwnerDrawVariable and NO border
3. Make the ListBox a child of the ListCtrl
4. Use OnSize to position and OnDrawItem to display the text
Note the OnDrawItem is using the text and some parameters from the CListCtrl
so be mindful of m_lbTest vs. m_lcTest.
*/
void CTestDlg::FillListCtrl()
{
// fill CListCtrl with some text
int x,y;
int ColCount;
CString csStr;
m_lbTest.SetParent(&m_lcTest);
ColCount=m_lcTest.GetHeaderCtrl()->GetItemCount();
for (y=0; y<10; y++) {
m_lcTest.InsertItem(y,"");
for (x=0; x<ColCount; x++) {
csStr.Format("y=%d x=%d",y,x);
m_lcTest.SetItemText(y,x,csStr);
}
m_lbTest.AddString("");
m_lbTest.SetItemHeight(y,20 + y*10);
}
}
void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
int x,y;
CRect rb;
if (m_lcTest.m_hWnd != NULL) {
y=0;
x=0;
m_lcTest.SetWindowPos(&wndTop,x,y,cx,cy-y,SWP_NOZORDER);
m_lcTest.GetHeaderCtrl()->GetClientRect(&rb);
x=0;
y+=rb.Height();
m_lbTest.SetWindowPos(&wndTop,x,rb.Height(),cx-x-4,cy-y-4,SWP_NOZORDER);
}
}
void CTestDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpD)
{
CString csStr;
CRect rc;
CRect rb;
UINT fmt;
LVCOLUMN lvc;
int x,y;
CDC dc;
int ColCount;
CPen cPen;
CopyRect(&rc,&lpD->rcItem);
dc.Attach(lpD->hDC);
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(GetStockObject(DEFAULT_GUI_FONT));
cPen.CreatecPen.CreatePen(PS_SOLID,1,RGB(192,192,192));
dc.SelectObject(cPen);
dc.SetTextColor(RGB(0,0,0));
if (m_lbTest.m_hWnd!=NULL && nIDCtl==IDC_LB_TEST) {
y=lpD->itemID;
if (y >= 0) {
rc.left+=5;
ColCount=m_lcTest.GetHeaderCtrl()->GetItemCount();
for (x=0; x<ColCount; x++){
rc.right=rc.left + m_lcTest.GetColumnWidth(x) - 10;
rb.top =rc.top+1;
rb.bottom=rc.bottom;
rb.left =rc.left - 5;
rb.right =rc.right + 4;
if ((lpD->itemState&ODS_SELECTED) != 0) dc.FillSolidRect(&rb,RGB(255,255,192)); // light yellow
else dc.FillSolidRect(&rb,RGB(255,255,255));
rb.top--;
dc.MoveTo(rb.left-1,rb.bottom);
dc.LineTo(rb.right, rb.bottom);
dc.LineTo(rb.right, rb.top);
dc.LineTo(rb.left-1,rb.top);
lvc.mask=LVCF_FMT;
m_lcTest.GetColumn(x,&lvc);
if ((lvc.fmt&LVCFMT_RIGHT) != 0) fmt=DT_RIGHT;
else if ((lvc.fmt&LVCFMT_CENTER) != 0) fmt=DT_CENTER;
else fmt=DT_LEFT;
fmt|=DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS | DT_NOPREFIX;
csStr=m_lcTest.GetItemText(y,x);
dc.DrawText(csStr,&rc,fmt);
rc.left=rc.right + 10;
}
}
}
cPen.DeleteObject();
dc.Detach();
}