李晓平/河北固安华北石油职工大学 ---- 无论是开发什么样的程序,数据输入是不可缺少的。快速地生成一个美观的输入界面无疑会大大提高程序开发的效率。系统原有的控件,往往不尽人意。在delphi中,如果针对某字段,可选的控件有DBLabel, DBEdit等;如果针对全表的输入,有DBGrid。使用Dbedit等控件时,用户必须全面安排各字段的位置,虽然能够达到美观的效果,但是,如果字段数多,无疑是很麻烦的。如果采用DBGrid,无论多少个字段,只用一个控件就够了,简单倒是简单,但是各字段是一字排列的,使用起来有点不方便。对于一般的用户来说,采用表格形式的录入,既方便,又美观。这就是本文所要解决的问题。 ---- 技术关键 ---- 本控件的主要功能是实现对数据库字段的编辑。按照一般的规律,控件中应包含TdataLink对象,还应该实现与TdataLink相关得一系列方法;但是,那样会耗费大量的代码。代码量越大,系统就越复杂,出错的可能性就越大。本控件的开发思路是以最少的代码实现最多的功能。所以,对数据字段的编辑直接使用TDBComboBox控件。 ---- 为了实现通用性,在控件内部维护了一个字段编辑控件数组和字段标题数组。如下: Editors: array of TDBComboBox; - >具体进行编辑所用的数据控件数组,动态生成Labels: array of TLabel; - >各字段的标题,动态生成---- 采用TDBComboBox优点是它不仅能具有一般的编辑功能,还能为各字段添加相应的提示信息。代码如下: { 为第I字段增加提示信息的方法}procedure TDBPanel.AddHits(ItemIndex: Integer; Hits: array of string);var m,n,i: Integer;begin n := Length(Editors); m := Length(Hits); if ItemIndex< n then begin for i:=0 to m-1 do Editors[ItemIndex].Items.Add(Hits[i]); end;end;---- 具体的应用是千差万别的,所以,控件还需要给程序员留有足够的事件处理接口,以实现具体应用时的特殊功能。这就需要在控件中定义一定的事件处理方法供用户实现。这里提供的是一个OnOkClick事件,即当所有字段编辑完成后所执行的处理。代码如下: OkButton: TButton; - >最后增加的确定按钮,用于实现提交动作。property OnOkClick: TNotifyEvent read FClick write FClick;---- 通过实现OnOKClick方法,用户可以完成提交、数据合理性检验等各种处理工作。另外一个需要特殊处理的是控制在各个字段间的转换。缺省的情况是用鼠标点击。但是,用户的习惯往往是用键盘中的"上、下、左、右"四个箭头键。要实现这一功能需定义以下两个方法: procedure AKeyPress(Sender: TObject; var Key: Char);procedure AKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);---- 将以上两个方法赋值给动态生成的Editors,从而实现对箭头键的响应。 ---- 不同的表字段数不同,有可能出现显示不下的情况,这就需要有滚动的功能。所以,在控件中插入了一个TscrollBox控件。最后一个需要注意的是动态控件的撤消及内存的释放。控件数组的撤消及内存的释放是有顺序的--与创建完全相反的顺序。否则会出错。 ----控件的使用 ---- 先将DBPanel控件放在窗体上,然后设置数据源属性、数据输入表格的列数等属性。在程序中,打开数据源后,调用创建数据编辑控件的方法即可。即: Query1.Open;- >打开数据源DBPanel1.CreateEditors; - >创建各字段的编辑控件DBPanel1.AddHits(0,['1111','11222','eeee']); - >为某字段设置提示信息DBPanel1.AddHits(1,['1111','11222','eeee']); - >为某字段设置提示信息该控件及示例程序在Win98+Delphi 5.0环境下调试通过。---- 附件:TDBPanel的源代码 unit DBPanel;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, dbctrls, stdctrls, db;type TDBPanel = class(TPanel) private { Private declarations } FLeft: Integer; FTop: Integer; maxTextLen: Integer; maxLabelLen: Integer; FScrollBox: TScrollBox; {滚动控件} FLineHeight: Integer; FClick: TNotifyEvent; Editors: array of TDBComboBox; - >具体进行编辑所用的数据控件数组,动态生成 Labels: array of TLabel; - >各字段的标题,动态生成 OkButton: TButton; - >最后增加的确定按钮,用于实现提交动作。 { 数据源} FDataSource: TDataSource; FColumns: Integer; - >输入表格的列数 protected { Protected declarations } procedure FreeEditors; - >释放数据输入控件的内存 public procedure CreateEditors;// (DS: TDataSource; ColCount: Integer);- >创建各字段的数据输入控件 constructor Create(AOwner: TComponent); override; destructor Destroy; override; procedure AKeyPress(Sender: TObject; var Key: Char); procedure AKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); procedure ClearHits(ItemIndex: Integer); procedure AddHits(ItemIndex: Integer; Hits: array of string); function Editor(Index: Integer): TDBComboBox; { Public declarations } published property LeftLimit: Integer read FLeft write FLeft default 10; property TopLimit: Integer read FTop write FTop default 10; property EditorLen: Integer read maxTextLen write maxTextLen; property LabelLen: Integer read maxLabelLen write maxLabelLen default 100; property LineHeight: Integer read FLineHeight write FLineHeight default 15; property OnOkClick: TNotifyEvent read FClick write FClick; property DataSource: TDataSource read FDataSource write FDataSource;- >数据源 property Columns: Integer read FColumns write FColumns;- >表列数 { Published declarations } end;procedure Register;implementationprocedure Register;begin RegisterComponents('Additional', [TDBPanel]);end;{ 为第I字段增加提示信息的方法}procedure TDBPanel.AddHits(ItemIndex: Integer; Hits: array of string);var m,n,i: Integer;begin n := Length(Editors); m := Length(Hits); if ItemIndex< n then begin for i:=0 to m-1 do Editors[ItemIndex].Items.Add(Hits[i]); end;end;procedure TDBPanel.AKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);begin if (Sender is TDBComboBox) then begin case Key of VK_Next: (Sender as TDBComboBox) .DataSource.DataSet.Next; VK_PRIOR: (Sender as TDBComboBox) .DataSource.DataSet.Prior; end; end;end;procedure TDBPanel.AKeyPress(Sender: TObject; var Key: Char);begin if (Sender is TDBComboBox) then beginif Key=#13 then (Owner as TForm).Perform(WM_NEXTDLGCTL, 0, 0); end;end;procedure TDBPanel.ClearHits(ItemIndex: Integer);var n: Integer;begin n := Length(Editors); if ItemIndex< n then Editors[ItemIndex].Items.Clear;end;constructor TDBPanel.Create(AOwner: TComponent);begin Inherited Create(AOWner); FLeft :=10; FTop := 10; maxTextLen := 100; maxLabelLen := 100; FLineHeight := 15;end;{ 创建各字段的数据输入控件的方法}procedure TDBPanel.CreateEditors;//(DS: TDataSource; ColCount: Integer);var i, n, RowCount: Integer; TextHeight: Integer;begin if DataSource.DataSet.Active then begin n := DataSource.DataSet.FieldCount; { 计算最大的标题长度及显示长度} DataSource.DataSet.First; { 计算高度} TextHeight := Canvas.TextHeight(DataSource .DataSet.Fields[0].DisplayLabel) + FLineHeight; //10; { 计算行列数} RowCount := n div Columns; if n mod Columns < > 0 then inc(RowCount); { 分配内存} FreeEditors; SetLength(Editors, n); SetLength(Labels, n); { 创建滚动盒} FScrollBox := TScrollBox.Create(Owner); FScrollBox.Parent := Self; FScrollBox.Align := alClient; { 创建编辑} for i:=0 to n-1 do begin { 创建标题} Labels[i] := TLabel.Create(Owner); Labels[i].Parent := FScrollBox; //Self;Labels[i].Caption := DataSource.DataSet.Fields[i].DisplayLabel; Labels[i].Left := FLeft + (maxLabelLen + maxTextLen + 10) * (i div RowCount); Labels[i].Width := maxLabelLen;Labels[i].Top := FTop + (i mod RowCount) * TextHeight + 5; { 创建编辑对象} Editors[i] := TDBComboBox.Create(Owner); Editors[i].Parent := FScrollBox; //Self; Editors[i].Left := Labels[i].Left + Labels[i].Width; Editors[i].Width := maxTextLen; Editors[i].Top := FTop + (i mod RowCount) * TextHeight; Editors[i].DataSource := DataSource;Editors[i].DataField := DataSource.DataSet.Fields[i].FieldName; Editors[i].OnKeyPress := AKeyPress; Editors[i].OnKeyDown := AKeyDown; end; { 创建Ok按钮} OkButton := TButton.Create(Owner); OkButton.Parent := FScrollBox; OkButton.Left := Editors[n-1].Left; OkButton.Top := Editors[n-1].Top + TextHeight; OkButton.Caption := '确定'; OKButton.OnClick := FClick; end;end;destructor TDBPanel.Destroy;begin FreeEditors; Inherited Destroy;end;function TDBPanel.Editor(Index: Integer): TDBComboBox;begin if Index< Length(Editors) then Result := Editors[Index] else Result := nil;end;procedure TDBPanel.FreeEditors;var i,n: Integer;begin { 内存的释放是要有顺序的!必须以创建的相反的顺序进行! 尤其是当组件之间有父子关系时} if OkButton< >nil then OkButton.Free; if Editors< >nil then begin n := Length(Editors); for i:=0 to n-1 do Editors[i].free; Editors := nil; n := Length(Labels); for i:=0 to n-1 do Labels[i].Free; Labels := nil; end; if FScrollBox< >nil then begin FScrollBox.Free; FScrollBox := nil; end;end;end.