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

📄 databaseview.cpp

📁 数据库索引的管理
💻 CPP
字号:
// DatabaseView.cpp : implementation of the CDatabaseView class
//

#include "stdafx.h"
#include "Database.h"

#include "DatabaseDoc.h"
#include "DatabaseView.h"

#include "DialogData.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView

IMPLEMENT_DYNCREATE(CDatabaseView, CFormView)

BEGIN_MESSAGE_MAP(CDatabaseView, CFormView)
	//{{AFX_MSG_MAP(CDatabaseView)
	ON_BN_CLICKED(IDC_BUTTON_FIND, OnButtonFind)
	ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
	ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
	ON_UPDATE_COMMAND_UI(IDC_BUTTON_FIND, OnUpdateButtonFind)
	ON_UPDATE_COMMAND_UI(IDC_BUTTON_NEW, OnUpdateButtonNew)
	ON_UPDATE_COMMAND_UI(IDC_BUTTON_DELETE, OnUpdateButtonDelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView construction/destruction

CDatabaseView::CDatabaseView()
	: CFormView(CDatabaseView::IDD)
{
	//{{AFX_DATA_INIT(CDatabaseView)
	m_ssn = 0.0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CDatabaseView::~CDatabaseView()
{
}

void CDatabaseView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDatabaseView)
	DDX_Text(pDX, IDC_EDIT_SSN, m_ssn);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDatabaseView message handlers

void CDatabaseView::OnInitialUpdate() 
{
	CFormView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class

}

void CDatabaseView::OnButtonFind() 
{
	CDatabaseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// record ssn user entered
	UpdateData(TRUE);

	// create the data dialog object
	CDialogData dlg;
	ASSERT(&dlg != NULL);

	// set the ptr to the open database set
	dlg.m_pSet = pDoc->GetRecSet();

	// create search string
	dlg.m_pSet->m_strFilter.Format("[SSN] = %9.0f", m_ssn);

	// requery the database
	dlg.m_pSet->Requery();

	// allow editing of database fields
	dlg.m_pSet->Edit();

	// if user edited data, update the database
	if (dlg.DoModal() == IDOK)
		dlg.m_pSet->Update();
}

void CDatabaseView::OnButtonDelete() 
{
	CDatabaseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// record ssn user entered
	UpdateData(TRUE);

	// create the data dialog object
	CDialogData dlg;
	ASSERT(&dlg != NULL);

	// set the ptr to the open database set
	dlg.m_pSet = pDoc->GetRecSet();

	// Verify deleting record with current SSN
	CString str;
	str.Format("%.0f", m_ssn);
	if ( (AfxMessageBox("Delete the record with the following SSN:  " + str, MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2)) == IDYES )
	{
		// create search string
		dlg.m_pSet->m_strFilter.Format("[SSN] = %9.0f", m_ssn);
		
		// requery the database
		dlg.m_pSet->Requery();
		
		// delete the returned record
		dlg.m_pSet->Delete();
	}
}

void CDatabaseView::OnButtonNew() 
{
	CDatabaseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// record ssn user entered
	UpdateData(TRUE);

	// create the data dialog object
	CDialogData dlg;
	ASSERT(&dlg != NULL);

	// set the ptr to the open database set
	dlg.m_pSet = pDoc->GetRecSet();

	// Verify adding new record and SSN
	CString str;
	str.Format("%.0f", m_ssn);
	if ( (AfxMessageBox("Add a new record using the following SSN:  " + str, MB_YESNO | MB_ICONQUESTION)) == IDYES )
	{
		// add a new record to the database with user-supplied SSN
		dlg.m_pSet->AddNew();
		dlg.m_pSet->m_SSN = m_ssn;
		dlg.m_pSet->Update();

		// create search string
		dlg.m_pSet->m_strFilter.Format("[SSN] = %9.0f", m_ssn);
		
		// requery the database
		dlg.m_pSet->Requery();
		
		// allow editing of database fields
		dlg.m_pSet->Edit();

		// if user clicks OK button, update the database
		if (dlg.DoModal() == IDOK)
			dlg.m_pSet->Update();
	}
}

void CDatabaseView::OnUpdateButtonFind(CCmdUI* pCmdUI) 
{
	// Enable Find Record button if database file has been opened
	CDatabaseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// Toolbar & Menu
	pCmdUI->Enable(pDoc->IsFileOpen());

	// Form view
	if (pDoc->IsFileOpen())
		GetDlgItem(IDC_BUTTON_FIND)->ModifyStyle(WS_DISABLED,NULL,0);
	else
		GetDlgItem(IDC_BUTTON_FIND)->ModifyStyle(NULL,WS_DISABLED,0);
}

void CDatabaseView::OnUpdateButtonNew(CCmdUI* pCmdUI) 
{
	// Enable New Record button if database file has been opened
	CDatabaseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// Toolbar & Menu
	pCmdUI->Enable(pDoc->IsFileOpen());

	// Form view
	if (pDoc->IsFileOpen())
		GetDlgItem(IDC_BUTTON_NEW)->ModifyStyle(WS_DISABLED,NULL,0);
	else
		GetDlgItem(IDC_BUTTON_NEW)->ModifyStyle(NULL,WS_DISABLED,0);
}

void CDatabaseView::OnUpdateButtonDelete(CCmdUI* pCmdUI) 
{
	// Enable Delete Record button if database file has been opened
	CDatabaseDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// Toolbar & Menu
	pCmdUI->Enable(pDoc->IsFileOpen());

	// Form view
	if (pDoc->IsFileOpen())
		GetDlgItem(IDC_BUTTON_DELETE)->ModifyStyle(WS_DISABLED,NULL,0);
	else
		GetDlgItem(IDC_BUTTON_DELETE)->ModifyStyle(NULL,WS_DISABLED,0);
}

⌨️ 快捷键说明

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