⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shopinglistview.bak

📁 AppWizard has created this ShopingList application for you. This application not only demonstrates
💻 BAK
字号:
// ShopingListView.cpp : implementation of the CShopingListView class
//

#include "stdafx.h"
#include "ShopingList.h"

#include "ShopingListDoc.h"
#include "ShopingListView.h"

// ja - included for dlgs
#include "InsertDlg.h"
#include "ChgStrNameDlg.h"

#include <stdlib.h>



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CShopingListView

IMPLEMENT_DYNCREATE(CShopingListView, CFormView)

BEGIN_MESSAGE_MAP(CShopingListView, CFormView)
	//{{AFX_MSG_MAP(CShopingListView)
	ON_COMMAND(ID_INSERT, OnInsert)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList)
	ON_COMMAND(ID_DELETE, OnDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShopingListView construction/destruction

CShopingListView::CShopingListView()
	: CFormView(CShopingListView::IDD)
{
	//{{AFX_DATA_INIT(CShopingListView)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CShopingListView::~CShopingListView()
{
	m_nListBoxIndex = 0;
}

void CShopingListView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CShopingListView)
	DDX_Control(pDX, IDC_LIST1, m_ListBox);
	//}}AFX_DATA_MAP
}

BOOL CShopingListView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CShopingListView diagnostics

#ifdef _DEBUG
void CShopingListView::AssertValid() const
{
	CFormView::AssertValid();
}

void CShopingListView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CShopingListDoc* CShopingListView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShopingListDoc)));
	return (CShopingListDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShopingListView message handlers

void CShopingListView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();

	// ja - init the data and fill the list control
	CreateListBoxHeader();
}

void CShopingListView::UpdateListBox()
{
	
	// ja - clear out all items in list box
	m_ListBox.DeleteAllItems();
	
	CString   sProductName, sTemp;
	int       nQty;
	CString   sStore1Price;
	CString   sStore2Price;
	CString   sStore3Price;
	CString   sStore4Price;

	// ja - get the pointers to the properties
	CCeDBProp *pPropDescription,
			  *pPropQty,
			  *pPropStore1,
			  *pPropStore2,
			  *pPropStore3,
			  *pPropStore4;

	// ja - handle to the record
	CCeDBRecord rec;
	
	GetDocument()->m_Mydb.Close();
	GetDocument()->m_Mydb.Open(DB_NAME);

	// ja - this will move the cursor to the first record
	GetDocument()->m_Mydb.SeekFirst();
	
	// ja - this will move the cursor from the record to the next position automaticly
	GetDocument()->m_Mydb.m_bAutoSeekNext = TRUE;

	// ja - get the number of records in the db
	/*int nNumOfRecs = 0;
	nNumOfRecs = (int)GetDocument()->m_Mydb.GetNumRecords(); 

	CString h;
	h.Format(_T("%i"),nNumOfRecs);
	AfxMessageBox(h);*
	
//T - here is where it messes up  nNumOfRecs = 21830
	// ja - if there are no records exit
	if (nNumOfRecs <= 0 || nNumOfRecs >= 20000)
		return;	*/
	
	// ja - this must be done outside out main loop
	/*for (int t = 0; t <= nNumOfRecs; t++){
		m_ListBox.InsertItem(t, NULL);
	}*/

	// ja - fill the list box with data
	//for (int i = 0; i < nNumOfRecs; i++){
	int i;
	i = 1;
	m_ListBox.InsertItem(0, NULL);
	while (!(GetDocument()->m_Mydb.m_bEOF)) 
	{
		
		GetDocument()->m_Mydb.ReadCurrRecord(&rec);
				
		// ja - get the values from the db
		pPropDescription  = rec.GetPropFromIdent(PROP_DESCRIPTION);
		pPropQty	      = rec.GetPropFromIdent(PROP_QTY);
		pPropStore1       = rec.GetPropFromIdent(PROP_STORE1);
		pPropStore2       = rec.GetPropFromIdent(PROP_STORE2);
		pPropStore3		  = rec.GetPropFromIdent(PROP_STORE3);
		pPropStore4		  = rec.GetPropFromIdent(PROP_STORE4);
		
		// ja - now put the values into out vars
		sProductName = pPropDescription->GetString(); 
		nQty         = pPropQty->GetUShort();		
		sStore1Price = pPropStore1->GetString();
		sStore2Price = pPropStore2->GetString();
		sStore3Price = pPropStore3->GetString();
		sStore4Price = pPropStore4->GetString();
		
	
		
		// ja - convert to CString and put into list box
		m_ListBox.SetItemText(i, 0, sProductName); 
		
		m_ListBox.InsertItem(i+1, NULL);
		
		sTemp.Format(_T("%i"),nQty);
		m_ListBox.SetItemText(i, 1, sTemp); 
				
		m_ListBox.SetItemText(i, 2, sStore1Price); 
		m_ListBox.SetItemText(i, 3, sStore2Price); 
		m_ListBox.SetItemText(i, 4, sStore3Price); 
		m_ListBox.SetItemText(i, 5, sStore4Price); 
		i++;
	}	

	CreateTotals();
}

void CShopingListView::CreateListBoxHeader()
{
	
	// ja - create headers for out list box
	// ja - we have to delete the cols before we recreate them
	m_ListBox.DeleteColumn(0);
	m_ListBox.InsertColumn( 0, _T("Product Name") , LVCFMT_LEFT, 175 );
	m_ListBox.DeleteColumn(1);
	m_ListBox.InsertColumn( 1, _T("Qty") , LVCFMT_LEFT, 50 );
	m_ListBox.DeleteColumn(2);
	m_ListBox.InsertColumn( 2, _T("Store 1") , LVCFMT_RIGHT, 95 );
	m_ListBox.DeleteColumn(3);
	m_ListBox.InsertColumn( 3, _T("Store 2") , LVCFMT_RIGHT, 95 );
	m_ListBox.DeleteColumn(4);
	m_ListBox.InsertColumn( 4, _T("Store 3") , LVCFMT_RIGHT, 95 );
	m_ListBox.DeleteColumn(5);
	m_ListBox.InsertColumn( 5, _T("Store 4") , LVCFMT_RIGHT, 95 );

	UpdateListBox();
	
}


void CShopingListView::OnInsert() 
{
	CInsertDlg dlg;
	if (dlg.DoModal() == IDOK){
		//ja - get values from dlg
		int      nQty = dlg.m_nQty;
		CString  sProductName = dlg.m_sPdtName;
		float fStore1Price = dlg.m_fStore1;
		float fStore2Price = dlg.m_fStore2;
		float fStore3Price = dlg.m_fStore3;
		float fStore4Price = dlg.m_fStore4;
				
		// ja - call our funtion in the doc to add a record
		GetDocument()->AddRecord(sProductName, nQty, fStore1Price, fStore2Price, fStore3Price, fStore4Price);
		
	}
	
	UpdateListBox();
	
}

// ja - we have to change the first param to a TBNOTIY
//		this function will store the "clicked" row for deleteing
void CShopingListView::OnClickList(TBNOTIFY* pNMHDR, LRESULT* pResult) 
{
	m_nListBoxIndex = pNMHDR->iItem;
	
	*pResult = 0;
}

void CShopingListView::OnDelete() 
{
	// ja - test the position in the array
	if (m_nListBoxIndex <= GetDocument()->m_Mydb.GetNumRecords()  && m_nListBoxIndex >= 0){

		// ja - handle to the record
		CCeDBRecord rec;
		
		GetDocument()->m_Mydb.SeekToIndex(m_nListBoxIndex);
		
		// ja - prompt for deleteing
		if (AfxMessageBox(_T("Are you sure you want to delete " 
			+ m_ListBox.GetItemText(m_nListBoxIndex, 0) + " ?"),MB_OKCANCEL) == IDOK){
			
			// ja - delete the record from the db
			GetDocument()->m_Mydb.DeleteCurrRecord();
			UpdateListBox();
		}	
	}	
}

void CShopingListView::CreateTotals()
{
	// ja - get the number of records in the db
	int nTotalsRow;
	/*nTotalsRow = (int)GetDocument()->m_Mydb.GetNumRecords(); */
	nTotalsRow = m_ListBox.GetItemCount();

	// ja - first we will add up all of the totals
	CString sTemp;
	int nTotalQty = 0;
	float fTotalStore1Price = 0; 
	float fTotalStore2Price = 0;
	float fTotalStore3Price = 0;
	float fTotalStore4Price = 0;

	for (int i = 0; i < nTotalsRow; i++){
	
		// ja - be sure to use functions to convert a wide string
		nTotalQty         += _ttoi(m_ListBox.GetItemText(i, 1)); 
		fTotalStore1Price += (float)wcstod(m_ListBox.GetItemText(i, 2),'\0'); 
		fTotalStore2Price += (float)wcstod(m_ListBox.GetItemText(i, 3),'\0'); 
		fTotalStore3Price += (float)wcstod(m_ListBox.GetItemText(i, 4),'\0'); 
		fTotalStore4Price += (float)wcstod(m_ListBox.GetItemText(i, 5),'\0'); 	
	
	 }
	
	// ja - now write to the totals row
	m_ListBox.SetItemText(nTotalsRow, 0, _T("TOTALS")); 
	
	sTemp.Format(_T("%i"),nTotalQty);
	m_ListBox.SetItemText(nTotalsRow, 1, sTemp); 
				
	sTemp.Format(_T("%.2f"),fTotalStore1Price);
	m_ListBox.SetItemText(nTotalsRow, 2, sTemp); 

	sTemp.Format(_T("%.2f"),fTotalStore2Price);
	m_ListBox.SetItemText(nTotalsRow, 3, sTemp); 
		
	sTemp.Format(_T("%.2f"),fTotalStore3Price);
	m_ListBox.SetItemText(nTotalsRow, 4, sTemp); 
		
	sTemp.Format(_T("%.2f"),fTotalStore4Price);
	m_ListBox.SetItemText(nTotalsRow, 5, sTemp); 

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -