?? parent.~cpp
字號:
//-----------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Parent.h"
//-----------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfmParent *TfmParent;
//-----------------------------------------------------------------
__fastcall TfmParent::TfmParent(TComponent* Owner)
: TForm(Owner)
{
this->SetButtonEnable(0);
}
//-----------------------------------------------------------------
void __fastcall TfmParent::FormClose(TObject *Sender, TCloseAction &Action)
{
// 刪除窗體并回收空間
Action = caFree;
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btFirstClick(TObject *Sender)
{
// 到首記錄
Table1->First();
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btPriorClick(TObject *Sender)
{
// 到前一記錄
Table1->Prior();
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btNextClick(TObject *Sender)
{
// 到下一記錄
Table1->Next();
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btLastClick(TObject *Sender)
{
// 到尾記錄
Table1->Last();
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btNewClick(TObject *Sender)
{
// 設置按鈕狀態
SetButtonEnable(1);
// 新增記錄
Table1->Append();
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btEditClick(TObject *Sender)
{
// 設置按鈕狀態
SetButtonEnable(1);
// 編輯記錄
Table1->Edit();
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btDeleteClick(TObject *Sender)
{
if (Application->MessageBox("是否刪除記錄?", "確定", MB_YESNO)
== mrYes)
{
DataSource1->Edit();
// 刪除記錄
Table1->Delete();
}
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btSaveClick(TObject *Sender)
{
// 保存記錄
Table1->Post();
// 設置按鈕狀態
SetButtonEnable(0);
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btCancelClick(TObject *Sender)
{
// 取消編輯
Table1->Cancel();
// 設置按鈕狀態
SetButtonEnable(0);
}
//-----------------------------------------------------------------
void __fastcall TfmParent::btExitClick(TObject *Sender)
{
this->Close();
}
//-----------------------------------------------------------------
// 設置各種按鈕的狀態
void TfmParent::SetButtonEnable(int nStatus)
{
if(nStatus == 1)
{
// 編輯狀態
btFirst->Enabled = false;
btPrior->Enabled = false;
btNext->Enabled = false;
btLast->Enabled = false;
btNew->Enabled = false;
btEdit->Enabled = false;
btDelete->Enabled = false;
btSave->Enabled = true;
btCancel->Enabled = true;
// Panel2的所有控件都是可輸入的
Panel2->Enabled = true;
DBGrid1->Enabled = false;
}
else
{
// 瀏覽狀態
btFirst->Enabled = true;
btPrior->Enabled = true;
btNext->Enabled = true;
btLast->Enabled = true;
btNew->Enabled = true;
btEdit->Enabled = true;
btDelete->Enabled = true;
btSave->Enabled = false;
btCancel->Enabled = false;
// Panel2的所有控件都是不可輸入的
Panel2->Enabled = false;
DBGrid1->Enabled = true;
}
}
//-----------------------------------------------------------------
// 回車查詢
void __fastcall TfmParent::Edit1KeyPress(TObject *Sender, char &Key)
{
if(Key == 13)
BitBtn1->Click() ;
}
//-------------------------------------------------------------
// 搜索按鈕對應查詢功能
void __fastcall TfmParent::BitBtn1Click(TObject *Sender)
{
Table1->Active = false;
Table1->Active = true;
Table1->Filter = "";
AnsiString szFilter;
// 設置條件
AnsiString szType;
TField* pField;
if(!Edit1->Text.IsEmpty() && !szCondition1.IsEmpty())
{
pField = Table1->FieldByName(szCondition1);
if( pField->DataType == ftSmallint ||
pField->DataType == ftInteger ||
pField->DataType == ftWord ||
pField->DataType == ftBoolean ||
pField->DataType == ftFloat ||
pField->DataType == ftCurrency ||
pField->DataType == ftDate)
szFilter = szCondition1 + " = " + Edit1->Text + " and ";
else
szFilter = szCondition1 + " = '" + Edit1->Text + "*' and ";
}
if(!Edit2->Text.IsEmpty() && !szCondition2.IsEmpty())
{
pField = Table1->FieldByName(szCondition2);
if( pField->DataType == ftSmallint ||
pField->DataType == ftInteger ||
pField->DataType == ftWord ||
pField->DataType == ftBoolean ||
pField->DataType == ftFloat ||
pField->DataType == ftCurrency ||
pField->DataType == ftDate)
szFilter = szCondition2 + " = " + Edit2->Text + " and ";
else
szFilter = szCondition2 + " = '" + Edit2->Text + "*' and ";
}
if(!Edit3->Text.IsEmpty() && !szCondition3.IsEmpty())
{
pField = Table1->FieldByName(szCondition3);
if( pField->DataType == ftSmallint ||
pField->DataType == ftInteger ||
pField->DataType == ftWord ||
pField->DataType == ftBoolean ||
pField->DataType == ftFloat ||
pField->DataType == ftCurrency ||
pField->DataType == ftDate)
szFilter = szCondition3 + " = " + Edit3->Text + " and ";
else
szFilter = szCondition3 + " = '" + Edit3->Text + "*' and ";
}
// 清除最后多余的and
szFilter = szFilter.SubString(1,szFilter.Length()-5);
Table1->Filtered = false;
Table1->Filter = szFilter;
Table1->Filtered = true;
lblCount->Caption = "記錄數: " + IntToStr(Table1->RecordCount) ;
}
//-------------------------------------------------------------
// 隱藏不用的查詢edit和lable
void TfmParent::SetSeekEditStatus()
{ if(!szCondition1.IsEmpty())
{
Label1->Visible = true;
Edit1->Visible = true;
Label1->Caption = szCondition1;
}
if(!szCondition2.IsEmpty())
{
Label2->Visible = true;
Edit2->Visible = true;
Label2->Caption = szCondition2;
}
if(!szCondition3.IsEmpty())
{
Label3->Visible = true;
Edit3->Visible = true;
Label3->Caption = szCondition3;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -