home *** CD-ROM | disk | FTP | other *** search
- unit td;
- {
- Sample ToDo list application for PC Plus's Delphi Workshop
- Author: Huw Collingbourne
- Compatibility: Delphi 3 Standard or above
-
- Changes/Additions to the application developed in the previous
- issue of PC Plus.
- * In DBGrid the data is now sorted when a column header is clicked
- * Filters have been added.
- * Drop-down PickLists added to selected fields
- * CheckBox toggles row-select or edit in grid
- * Button by Dates loads Calendar for date-picking
- * 2nd table of categories has been added
- * sub-Form lets user pick date from calendar (when editing in Grid)
- * sub-Form lets user manage catagories (add, delete or select)
- * DBLookupComboBox displays picklist of Types from Categories table
-
- NOTE: In a finished application you would need to add exception-handling to
- recover gracefully from invalid input (e.g. if the user enters an
- invalid value into one of the columns of the DBGrid).
- }
- interface
-
- uses
- SysUtils, Windows, Messages, Classes, Graphics, Controls,
- StdCtrls, Forms, DBCtrls, DB, DBTables, Mask, ExtCtrls, ComCtrls, Grids,
- DBGrids, Dialogs;
-
- type
- TToDoForm = class(TForm)
- ToDoTableItemNumber: TAutoIncField;
- ToDoTableItem: TStringField;
- ToDoTablePriority: TSmallintField;
- ToDoTableType: TStringField;
- ToDoTableDateentered: TDateField;
- ToDoTableDatedue: TDateField;
- ToDoTableDone: TBooleanField;
- ToDoTableNotes: TBlobField;
- ScrollBox: TScrollBox;
- Label1: TLabel;
- EditItemNumber: TDBEdit;
- Label2: TLabel;
- EditItem: TDBEdit;
- Label4: TLabel;
- Label5: TLabel;
- Label6: TLabel;
- Label7: TLabel;
- CheckBoxDone: TDBCheckBox;
- Label8: TLabel;
- DBNavigator: TDBNavigator;
- Panel1: TPanel;
- ToDoDataSource: TDataSource;
- Panel2: TPanel;
- ToDoTable: TTable;
- DBRichEdit1: TDBRichEdit;
- DTPDateEntered: TDateTimePicker;
- DBRadioGroup1: TDBRadioGroup;
- DTPDatedue: TDateTimePicker;
- Panel3: TPanel;
- DBGrid1: TDBGrid;
- GridEditCB: TCheckBox;
- GroupBox1: TGroupBox;
- Label3: TLabel;
- Label9: TLabel;
- OverdueCB: TCheckBox;
- ApplyFilterBtn: TButton;
- PriorityCombo: TComboBox;
- RemoveFilterBtn: TButton;
- TypeCombo: TComboBox;
- DBLookupComboBox1: TDBLookupComboBox;
- CategoriesDataSource: TDataSource;
- CategoriesTable: TTable;
- CategoriesTableType: TStringField;
- LoadCatAddBtn: TButton;
- procedure FormCreate(Sender: TObject);
- procedure DTPDateEnteredChange(Sender: TObject);
- procedure DBRichEdit1KeyUp(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- procedure ItemBtnClick(Sender: TObject);
- procedure DoneBtnClick(Sender: TObject);
- procedure EditDateenteredExit(Sender: TObject);
- procedure DTPDatedueChange(Sender: TObject);
- procedure ToDoTableAfterScroll(DataSet: TDataSet);
- procedure ToDoTableAfterInsert(DataSet: TDataSet);
- procedure PriorityBtnClick(Sender: TObject);
- procedure DBGrid1TitleClick(Column: TColumn);
- procedure ApplyFilterBtnClick(Sender: TObject);
- procedure RemoveFilterBtnClick(Sender: TObject);
- procedure GridEditCBClick(Sender: TObject);
- procedure DBGrid1EditButtonClick(Sender: TObject);
- procedure LoadCatAddBtnClick(Sender: TObject);
- private
- { private declarations }
- public
- { public declarations }
- function AddAnd( s : string ) : string;
- procedure ShowCatAddForm;
- procedure CreateTypeComboList;
- end;
-
- var
- ToDoForm: TToDoForm;
-
- const
- // These match the TColumn.ID values that identify columns. The
- // ID property (unlike the Index property) stays the same even if
- // columns are moved or deleted.
- ITEMID = 0;
- PRIORITYID = 1;
- TYPEID = 2;
- DATEENTEREDID = 3;
- DATEDUEID = 4;
- DONEID = 5;
-
- PRIORITIES : set of char = ['1','2','3'];
-
- DEFAULT_DBGRID_OPTIONS = [dgTitles..dgCancelOnExit];
- EDITING_DBGRID_OPTIONS = [dgEditing..dgTabs,dgAlwaysShowSelection..dgCancelOnExit];
-
- implementation
-
- uses dt, catadd;
-
- {$R *.DFM}
- procedure TToDoForm.CreateTypeComboList;
- // build Items list for TypeCombo list to match current Categories table
- var
- i : integer;
- begin
- TypeCombo.Clear;
- CategoriesTable.First; // initially, move to 1st record in table
- for i := 0 to CategoriesTable.RecordCount-1 do
- begin
- TypeCombo.Items.Insert(0,CategoriesTableType.Value);
- CategoriesTable.Next;
- end;
- TypeCombo.Items.Insert(0,'Any');
- TypeCombo.Text := 'Any';
- end;
-
- function TToDoForm.AddAnd( s : string ) : string;
- begin
- if s = '' then
- result := s
- else
- result := s + ' and ';
- end;
-
- procedure TToDoForm.FormCreate(Sender: TObject);
- begin
- ToDoTable.Open;
- CreateTypeComboList;
- DBGrid1.Options := DEFAULT_DBGRID_OPTIONS;
- end;
-
- procedure TToDoForm.DTPDateEnteredChange(Sender: TObject);
- begin
- ToDoDataSource.Edit; // put into edit mode and assign the date
- ToDoTableDateentered.Value := DTPDateEntered.Date;
- end;
-
- procedure TToDoForm.DBRichEdit1KeyUp(Sender: TObject; var Key: Word;
- Shift: TShiftState);
- begin
- if (ssCtrl in Shift) and (Chr(Key) = 'B') then
- with DBRichEdit1.SelAttributes do
- begin
- if fsBold in Style then
- Style := Style - [fsBold]
- else Style := Style+[fsBold];
- end
- else
- if (ssCtrl in Shift) and (Chr(Key) = 'U') then
- with DBRichEdit1.SelAttributes do
- begin
- if fsUnderline in Style then
- Style := Style - [fsUnderline]
- else Style := Style+[fsUnderline];
- end
- end;
-
- procedure TToDoForm.ItemBtnClick(Sender: TObject);
- begin
- if ToDoTable.IndexName = 'ItemIDX' then
- ToDoTable.IndexName := ''
- else ToDoTable.IndexName := 'ItemIDX';
- end;
-
- procedure TToDoForm.DoneBtnClick(Sender: TObject);
- begin
- if ToDoTable.IndexName = 'DoneIDX' then
- ToDoTable.IndexName := ''
- else ToDoTable.IndexName := 'DoneIDX';
- end;
-
- procedure TToDoForm.PriorityBtnClick(Sender: TObject);
- begin
- if ToDoTable.IndexName = 'PriorityIDX' then
- ToDoTable.IndexName := ''
- else ToDoTable.IndexName := 'PriorityIDX';
- end;
-
- procedure TToDoForm.EditDateenteredExit(Sender: TObject);
- begin
- DTPDateEntered.Date := ToDoTableDateentered.Value;
- end;
-
- procedure TToDoForm.DTPDatedueChange(Sender: TObject);
- begin
- ToDoDataSource.Edit; // put into edit mode and assign the date
- ToDoTableDatedue.Value := DTPDatedue.Date;
- end;
-
- procedure TToDoForm.ToDoTableAfterScroll(DataSet: TDataSet);
- begin
- DTPDateEntered.Date := ToDoTableDateentered.Value;
- DTPDateDue.Date := ToDoTableDatedue.Value;
- end;
-
- procedure TToDoForm.ToDoTableAfterInsert(DataSet: TDataSet);
- begin
- ToDoTableDateentered.Value := Now;
- ToDoTableDatedue.Value := Now;
- ToDoTablePriority.Value := 2;
- end;
-
-
- procedure TToDoForm.DBGrid1TitleClick(Column: TColumn);
- // Sort data according to which column header has been clicked
- begin
- case Column.ID of
- ITEMID: ToDoTable.IndexName := 'ItemIDX';
- PRIORITYID: ToDoTable.IndexName := 'PriorityIDX';
- TYPEID: ToDoTable.IndexName := 'TypeIDX';
- DATEENTEREDID: ToDoTable.IndexName := 'DateEnteredIDX';
- DATEDUEID: ToDoTable.IndexName := 'DateDueIDX';
- DONEID:ToDoTable.IndexName := 'DoneIDX';
- else ShowMessage( 'Error: Invalid Column Header was clicked!');
- end;
- end;
-
- procedure TToDoForm.ApplyFilterBtnClick(Sender: TObject);
- var
- FS : string;
- begin
- FS := '';
- // Try to apply a filter if optiuons have been set
- begin // Create a Filter String, FS
- if OverdueCB.Checked then FS := '(''Done'' = False) and (''Date due'' < ''' + DateToStr(Now) +''')';
- if PriorityCombo.Text[1] in PRIORITIES then FS := AddAnd(FS) + ' (''Priority'' = ' + PriorityCombo.Text[1] +' )';
- if CategoriesTable.FindKey( [TypeCombo.Text]) then FS := AddAnd(FS) + ' (''Type'' = '''+TypeCombo.Text +''' )';
- // Now apply the filter
- // ShowMessage( '['+FS+']' ); //!! Uncomment this to preview filter string
- ToDoTable.Filter := FS;
- ToDoTable.Filtered := true;
- end;
-
- end;
-
- procedure TToDoForm.RemoveFilterBtnClick(Sender: TObject);
- begin
- ToDoTable.Filtered := false;
- end;
-
- procedure TToDoForm.GridEditCBClick(Sender: TObject);
- begin
- if GridEditCB.Checked then
- begin
- DBGrid1.Options := EDITING_DBGRID_OPTIONS;
- ActiveControl := DBGrid1;
- end
- else DBGrid1.Options := DEFAULT_DBGRID_OPTIONS;
- end;
-
- procedure TToDoForm.DBGrid1EditButtonClick(Sender: TObject);
- var
- d : TDateTime;
- begin
- if (DBGrid1.SelectedField.FieldName = 'Type') then
- ShowCatAddForm
- else
- if (DBGrid1.SelectedField.FieldName = 'Date entered') or
- (DBGrid1.SelectedField.FieldName = 'Date due') then
- begin
- // if date cell is blank, d := today's date.
- // else d := date in cell
- if DBGrid1.SelectedField.IsNull then
- d := Now
- else
- d := DBGrid1.SelectedField.Value;
- // display calendar form and set its date(s) to d
- with CalendarForm do
- begin
- DateTimePicker1.Date := d;
- SetCalendarDate( d );
- end;
- // if OK button was clicked, set cell to the selected date
- if CalendarForm.ShowModal = mrOK then
- begin
- ToDoDataSource.Edit; // put dataset into edit mode
- DBGrid1.SelectedField.Value := CalendarForm.DateTimePicker1.Date;
- end;
- end;
- end;
-
- procedure TToDoForm.ShowCatAddForm;
- begin
- if CatAddForm.ShowModal = mrOK then
- begin
- ToDoDataSource.Edit; // put into edit mode and assign the category
- ToDoTableType.Value := CategoriesTableType.Value;
- end;
- end;
-
- procedure TToDoForm.LoadCatAddBtnClick(Sender: TObject);
- begin
- ShowCatAddForm;
- end;
-
- end.
-