分享
 
 
 

能够处理任何数据库字段的Panel

王朝delphi·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

unit DBPanel;

interface

uses

windows, 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;

file://- >具体进行编辑所用的数据控件数组,动态生成

labels: array of tlabel;

file://- >各字段的标题,动态生成

okbutton: tbutton;

file://- >最后增加的确定按钮,用于实现提交动作。

{ 数据源}

fdatasource: tdatasource;

fcolumns: integer;

file://- >输入表格的列数

protected

{ protected declarations }

procedure freeeditors;

file://- >释放数据输入控件的内存

public

procedure createeditors(ds: tdatasource; colcount: integer);

file://- >创建各字段的数据输入控件

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;

file://数据源

property columns: integer read

fcolumns write fcolumns;//- >表列数

{ published declarations }

end;

procedure Register;

implementation

{ 为第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 begin

if 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; file://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; file://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; file://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;

procedure Register;

begin

RegisterComponents('OK', [TDBPanel]);

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有