I am going to use CListCtrl in a MFC Modal Dialog to list the invoice items to the user. I initiate the column of CListCtrl in the OnInitDialog stage and successful. And I try to update the content of CListCtrl in the DoDataExchange stage but failed. I debug the code and found it may because of NULL m_hWnd of CListCtrl. The code is as below. Any one knows what is right way to add the items to the CListCtrl so the user can see the content in the CListCtrl when user opens the Dialog?
Code:
// I initiate the column of CListCtrl in the OnInitDialog stage, and successful
BOOL CReceiptDlg::OnInitDialog(
)
{
CDialog::OnInitDialog();
m_ReceiptStatListCtrl.Inse
rtColumn(0
, "ID", LVCFMT_CENTER, 50, 0);
m_ReceiptStatListCtrl.Inse
rtColumn(1
, "Entry Type", LVCFMT_CENTER, 80, 1);
m_ReceiptStatListCtrl.Inse
rtColumn(2
, "Date Time", LVCFMT_CENTER, 120, 2);
m_ReceiptStatListCtrl.Inse
rtColumn(3
, "Description", LVCFMT_CENTER, 150, 3);
m_ReceiptStatListCtrl.Inse
rtColumn(4
, "Amount", LVCFMT_CENTER, 100, 4);
return TRUE;
}
void CReceiptDlg::DoDataExchang
e(CDataExc
hange* pDX)
{
if (!pDX->m_bSaveAndValidate)
{
// if the exchange data is to update the control, I will populate the data to contrl before update
UpdateReceiptStat();
}
CDialog::DoDataExchange(pD
X);
// Update the other controls successfully
DDX_Text(pDX, IDC_RECEIPT_ID, m_ReceiptId);
DDX_Text(pDX, IDC_RECEIPT_DATE_TIME, m_StrCreationDateTime);
DDX_Text(pDX, IDC_RECEIPT_CHARGE, m_StrReceiptCharge);
DDX_Text(pDX, IDC_RECEIPT_PAYMENT, m_StrReceiptPayment);
DDX_Text(pDX, IDC_RECEIPT_ADJ, m_StrReceiptAdj);
DDX_Text(pDX, IDC_RECEIPT_TOTAL, m_StrReceiptTotal);
// Update the CListCtrl
DDX_Control(pDX, IDC_RECEIPT_STAT_LIST, m_ReceiptStatListCtrl);
}
// The function to update the receipt statement data
void CReceiptDlg::UpdateReceipt
Stat()
{
// Clear the memory of the old receipt statements
if (m_pReceipt->nNumOfReceipt
Stat > 0)
{
delete[] m_pReceiptStat;
m_pReceiptStat = NULL;
}
// Read the receipt statements from Database
m_pReceiptStat = theApp.adoAccess.GetReceip
tStat(m_pR
eceipt, NULL, time(NULL),100);
// Delete the content in the CListCtrl if has
if (m_ReceiptStatListCtrl.m_h
Wnd != NULL)
{
m_ReceiptStatListCtrl.Dele
teAllItems
();
}
// Insert items to the CListCtrl control
CString StrDateTime;
CString StrDoubleValue;
tm *tmDateTime = NULL;
RECEIPT_STAT *pFirstReceiptStat = m_pReceiptStat;
// Loop to insert the items to the CListCtrl
for (int i=0; i<m_pReceipt->nNumOfReceip
tStat; pFirstReceiptStat++)
{
StrDoubleValue.Format("%d"
, i);
m_ReceiptStatListCtrl.Inse
rtItem(i, CSPTR(StrDoubleValue)); // Error here
// Entry Type Column
switch((ENTRY_TYPE)m_pRece
iptStat->n
EntryType)
{
case Charge:
m_ReceiptStatListCtrl.SetI
temText(i,
1, CString("Charge")); // Error here
break;
case Payment:
m_ReceiptStatListCtrl.SetI
temText(i,
1, CString("Payment")); // Error here
break;
case Adjustement:
m_ReceiptStatListCtrl.SetI
temText(i,
1, CString("Adjustment")); // Error here
break;
case Refund:
m_ReceiptStatListCtrl.SetI
temText(i,
1, CString("Refund")); // Error here
break;
default:
m_ReceiptStatListCtrl.SetI
temText(i,
1, CString("")); // Error here
break;
}
// DateTime Column
tmDateTime = localtime(&m_pReceiptStat-
>lStartDat
eTime);
StrDateTime.Format(" %.2d/%.2d/%.4d %.2d:%.2d:%.2d", tmDateTime->tm_mday,
tmDateTime->tm_mon, tmDateTime->tm_year + 1900,
tmDateTime->tm_hour, tmDateTime->tm_min, tmDateTime->tm_sec);
m_ReceiptStatListCtrl.SetI
temText(i,
2, StrDateTime); // Error here
// Description, Amount
m_ReceiptStatListCtrl.SetI
temText(i,
3, m_pReceiptStat->nDescripti
on); // Error here
StrDoubleValue.Format("%.2
f", m_pReceiptStat->dAmount);
m_ReceiptStatListCtrl.SetI
temText(i,
4, StrDoubleValue); // Error here
i++;
} // end of loop
}
Start Free Trial