RTFormwork/项目代码/RTBasicsV1/C02纱线加弹检验/DbPanel/JKFootpanel.pas

340 lines
9.7 KiB
ObjectPascal
Raw Permalink Normal View History

2024-07-07 09:35:27 +08:00
unit JKFootpanel;
interface
uses
Windows, Messages, SysUtils, Classes,Graphics, Controls, Forms, Dialogs,
ExtCtrls, dbctrls, stdctrls, db, ADODB, ComCtrls;
type
TEditorstyle = (TsDbMemo,TsDBcombox,Tsdbedit);
type
TJKFootpanel = class(TPanel)
private
{ Private declarations }
FLeft: Integer;
FTop: Integer;
maxTextLen: Integer;
maxLabelLen: Integer;
FScrollBox: TScrollBox; {<7B><><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD>}
FLineHeight: Integer;
FEditorstyle:Teditorstyle;
FTitleVisible :Boolean;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>
MemoEditors :array of TDBMemo;
comEditors :array of TDBCombobox;
edEditors :array of TDBedit;
Labels :array of TLAbel; //<2F>ֶα<D6B6><CEB1><EFBFBD><E2A3AC>̬<EFBFBD><CCAC><EFBFBD><EFBFBD>
FDataSource :TDataSource; // <20><><EFBFBD><EFBFBD>Դ
FColumns :Integer; //<2F><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
procedure FreeEditors; //<2F>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC><EFBFBD><EFBFBD>ڴ<EFBFBD>
procedure AKeyDown(Sender :TObject; var Key: Word; Shift:TShiftState);
procedure AKeyPress(Sender :TObject; var Key: Char);
function comEditor(Index :Integer):TDBComboBox;
function edEditor(Index :Integer):TDBedit;
function MemoEditor(Index :Integer): TDBMemo;
protected
{ Protected declarations }
public
constructor Create(AOwner :TComponent); override;
destructor Destroy; override;
procedure Setedit(Value :TEditorstyle);
procedure ClearHits(ItemIndex :Integer);
procedure AddHits(ItemIndex :Integer; Hits :array of string);
Function CreateEditors(DS :TDataSource; ColCount :Integer) :integer; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶε<D6B6><CEB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD>
{ Public declarations }
published
property LimitLeft :Integer read FLeft write FLeft default 10;
property LimitTop :Integer read FTop write FTop default 10;
property Editorstyle :TEditorstyle read FEditorstyle write FEditorstyle default Tsdbedit;
property EditorWidth :Integer read maxTextLen write maxTextLen default 100;
property TitleWidth :Integer read maxLabelLen write maxLabelLen default 100;
property LineHeight :Integer read FLineHeight write FLineHeight default 15;
property TitleVisible :Boolean read FTitleVisible write FTitleVisible default True;
property DataSource :TDataSource read FDataSource write FDataSource; //<2F><><EFBFBD><EFBFBD>Դ
property Columns :Integer read FColumns write FColumns default 4;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{ Published declarations }
end;
procedure Register;
implementation
constructor TJKFootpanel.Create(AOwner: TComponent);
begin
Inherited Create(AOWner);
FLeft :=16;
FTop := 10;
maxTextLen := 100;
maxLabelLen := 100;
FLineHeight := 15;
FTitleVisible := True;
Editorstyle := Tsdbedit;
end;
procedure Register;
begin
RegisterComponents('Data Controls', [TJKFootpanel]);
end;
procedure TJKFootpanel.Setedit(Value : TEditorstyle);
begin
if FEditorstyle <> Value then
begin
FEditorstyle := Value;
Invalidate;
end;
end;
{ Ϊ<><CEAA>I<EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>Ϣ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>}
procedure TJKFootpanel.AddHits(ItemIndex:
Integer; Hits: array of string);
var
m,n,i: Integer;
begin
if FEditorstyle = TsDBcombox then
begin
n := Length(comEditors);
m := Length(Hits);
if ItemIndex< n then
for i:=0 to m-1 do
comEditors[ItemIndex].Items.Add(Hits[i]);
end
else if FEditorstyle = TsDBEdit then
begin
n := Length(edEditors);
m := Length(Hits);
if ItemIndex< n then
for i:=0 to m-1 do
edEditors[ItemIndex].Hint:= Hits[i];
end
else if FEditorstyle = TsDBMemo then
begin
n := Length(memoEditors);
m := Length(Hits);
if ItemIndex< n then
for i:=0 to m-1 do
memoEditors[ItemIndex].Hint:= Hits[i];
end;
end;
procedure TJKFootpanel.AKeyPress(Sender: TObject; var Key: Char);
begin
if (Sender is TDBComboBox) or (Sender is TDBedit) or (Sender is TDBMemo) then
if Key=#13 then
(Owner as TForm).Perform(WM_NEXTDLGCTL, 0, 0);
end;
procedure TJKFootpanel.ClearHits(ItemIndex: Integer);
var
n: Integer;
begin
if FEditorstyle = TsDBcombox then
begin
n := Length(comEditors);
if ItemIndex< n then comEditors[ItemIndex].Items.Clear;
end
else if FEditorstyle = TsDBEdit then
begin
n := Length(edEditors);
if ItemIndex< n then edEditors[ItemIndex].Hint:='';;
end
else if FEditorstyle = TsDBMemo then
begin
n := Length(MemoEditors);
if ItemIndex< n then MemoEditors[ItemIndex].Hint:='';;
end;
end;
{ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶε<D6B6><CEB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC>ķ<EFBFBD><C4B7><EFBFBD>}
Function TJKFootpanel.CreateEditors(DS: TDataSource; ColCount: Integer):Integer ;
var
i, n, RowCount: Integer;
TextHeight: Integer;
begin
result := 120;
if DataSource = nil then exit;
if not DataSource.DataSet.Active then exit;
Columns := ColCount;
if Columns = 0 then exit;
n := DataSource.DataSet.fieldCount;
if n > 0 then
begin
DataSource.DataSet.DisableControls;
if maxLabelLen < maxTextLen then
maxTextLen := maxLabelLen;
{ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>}
DataSource.DataSet.First;
{ <20><><EFBFBD><EFBFBD><EFBFBD>߶<EFBFBD>}
TextHeight := Canvas.TextHeight(DataSource.DataSet.Fields[0].DisplayLabel) + FLineHeight; //10;
{ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}
if Columns > n then
Columns := n
else
Columns := ColCount;
RowCount := n div Columns;
if n mod Columns <> 0 then inc(RowCount);
{ <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>}
FreeEditors;
SetLength(Labels,n);
if FEditorstyle = TsDBcombox then
SetLength(comEditors,n)
else if FEditorstyle = TsDBEdit then
SetLength(edEditors,n)
else
SetLength(MemoEditors,n);
{ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}
FScrollBox := TScrollBox.Create(Owner);
FScrollBox.Visible := False;
FScrollBox.Parent := Self;
FScrollBox.Align := alClient;
//FScrollBox.Color := clSkyBlue;
FScrollBox.OnDblClick := OnDblClick;
{ <20><><EFBFBD><EFBFBD><EFBFBD>༭}
for i := 0 to n - 1 do
begin
{ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>}
Labels[i] := TLabel.Create(Owner);
Labels[i].visible := TitleVisible;
Labels[i].Parent := FScrollBox;
Labels[i].Caption := DataSource.DataSet.Fields[i].FieldName;
Labels[i].Left := FLeft + (maxLabelLen + 16) * (i div RowCount)+ 2; //+maxTextLen
if FEditorstyle = TsDBMemo then
begin
Labels[i].Width := maxLabelLen;
Labels[i].Top := FTop + (i mod RowCount) * (TextHeight*2+4) + 12;
end
else
begin
Labels[i].Width := maxLabelLen;
Labels[i].Top := FTop + (i mod RowCount) * (TextHeight*1 +12)
end;
{ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2>ʾ<EFBFBD><CABE><EFBFBD>ݶ<EFBFBD><DDB6><EFBFBD>}
if FEditorstyle = TsDBcombox then
begin
comEditors[i] := TDBComboBox.Create(Owner);
comEditors[i].Parent := FScrollBox; //Self;
comEditors[i].Left := Labels[i].Left; //+ Labels[i].Width;
comEditors[i].Width := maxTextLen;
comEditors[i].Top := Labels[i].Top+15;
comEditors[i].DataSource := DataSource;
comEditors[i].DataField := DataSource.DataSet.Fields[i].FieldName;
comEditors[i].OnKeyPress := AKeyPress;
comEditors[i].OnKeyDown := AKeyDown;
comEditors[i].Font.Color := ClBlue;
end
else if FEditorstyle = TsDBEdit then
begin
edEditors[i] := TDBedit.Create(Owner);
edEditors[i].Parent := FScrollBox;
edEditors[i].Left := Labels[i].Left; //+ Labels[i].Width;
edEditors[i].Width := maxTextLen;
edEditors[i].Top := Labels[i].Top+15;
edEditors[i].DataSource := DataSource;
edEditors[i].DataField := DataSource.DataSet.Fields[i].FieldName;
edEditors[i].OnKeyPress := AKeyPress;
edEditors[i].OnKeyDown := AKeyDown;
edEditors[i].Font.Color := ClBlue;
end
else
begin
MemoEditors[i] := TDBmemo.Create(Owner);
MemoEditors[i].Parent := FScrollBox;
MemoEditors[i].Left := Labels[i].Left;
MemoEditors[i].Width := maxTextLen;
MemoEditors[i].Top := Labels[i].Top+15;
MemoEditors[i].DataSource := DataSource;
MemoEditors[i].DataField := DataSource.DataSet.Fields[i].FieldName;
MemoEditors[i].OnKeyPress := AKeyPress;
MemoEditors[i].OnKeyDown := AKeyDown;
MemoEditors[i].Font.Color := ClBlue;
end;
end;
if FEditorstyle = TsDBMemo then
result := RowCount*TextHeight*4 +20
else
result := RowCount*TextHeight*2 +20;
DataSource.DataSet.EnableControls;
FScrollBox.Visible := True;
end;
end;
destructor TJKFootpanel.Destroy;
begin
FreeEditors;
Inherited Destroy;
end;
function TJKFootpanel.comEditor(Index: Integer): TDBComboBox;
begin
if Index< Length(comEditors) then Result := comEditors[Index]
else Result := nil;
end;
function TJKFootpanel.edEditor(Index: Integer): TDBedit;
begin
if Index < Length(edEditors) then Result := edEditors[Index]
else Result := nil;
end;
function TJKFootpanel.MemoEditor(Index: Integer): TDBMemo;
begin
if Index< Length(MemoEditors) then Result := MemoEditors[Index]
else Result := nil;
end;
// <20>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>Ҫ<EFBFBD><D2AA>˳<EFBFBD><CBB3><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B7B4>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>и<EFBFBD><D0B8>ӹ<EFBFBD>ϵʱ
procedure TJKFootpanel.FreeEditors;
begin
if FScrollBox <> nil then
begin
comEditors := nil;
edEditors := nil;
MemoEditors := nil;
FScrollBox.Free;
FScrollBox := nil;
end;
end;
procedure TJKFootpanel.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
else if (Sender is TDBedit) then
begin
case Key of
VK_Next: (Sender as TDBedit).DataSource.DataSet.Next;
VK_PRIOR: (Sender as TDBedit).DataSource.DataSet.Prior;
end;
end
else if (Sender is TDBMemo) then
begin
case Key of
VK_Next: (Sender as TDBMemo).DataSource.DataSet.Next;
VK_PRIOR: (Sender as TDBMemo).DataSource.DataSet.Prior;
end;
end;
}
end;
end.