我在编程里用到的函数,归在一个类中(未完)
来源:互联网 发布:网络礼仪 编辑:程序博客网 时间:2024/05/02 00:52
把在编程中遇到的一些代码整理在一个类中,这样可以提高代码的重用性
------------------------------------------------------------------------------------------------------------
unit PublicFunctionUnit;
interface
uses sysutils,dateutils,db,stdctrls,dbTables,windows,extCtrls,comctrls;
type
TPublicFunction = class
public
// 根据 日期生成 06030010 形式的 id 号 要传入一个打开该表的数据集以及这保存 id 的字段名
class function GetAutoCreateIDWithDate(theDataSet:TdataSet;FieldName:string):string;
// 给 combobox 的 item 添加项目 传入一个 query combobox 以及 sql 语句
class procedure AddItemToCombobox(fQuery:TQuery;fCombobox:TCombobox;SqlStr:string);
// 用 DBtable 的 delete 来删除记录 table 和 query 都属于 DBTable
class procedure DeleteRecordWithDBTable(aDataset:TDataSet);
// 清除 panel 面板上的所有的 Edit 里的 Text 值
Class Procedure ClearTextFromPanel(thePanel:TPanel);
// 给 listView 创建列,根据数组来创建
Class procedure CreateListViewColumn(theColumns:array of string;theColumnWidth:array of integer;
theListView:TListView);
// 给 listView 添加数据,根据打开的 Dataset; 是否显示 checkbox
Class procedure AddDataToListView(aDataSet:TDataset;aListView:TListview;shCheckBox:boolean);
end;
implementation
uses FocObjectUnit;
class function TPublicFunction.GetAutoCreateIDWithDate(theDataSet:TdataSet;FieldName:string):string;
var
sOrderID,sTemp,sYear,sMonth:string;
begin
if not theDataSet.Active then theDataSet.Open;
sYear:=Copy(IntToStr(YearOf(Now)),3,2);
sMonth:=IntToStr(MonthOf(Now));
sMonth:=Copy('0',length(sMonth),2-length(sMonth))+sMonth;
if theDataSet.IsEmpty then
begin
sOrderID:=sYear+sMonth+'0001';
end else
begin
theDataSet.Last;
sTemp:=Copy(theDataSet.FieldByName(FieldName).AsString,1,4);
if (sTemp=(sYear+sMonth)) then
begin
sTemp:=IntToStr(StrToInt(Copy(theDataSet.FieldByName(FieldName).AsString,5,4))+1);
sTemp:=Copy('000',length(sTemp),4-length(sTemp))+sTemp;
sOrderID:=sYear+sMonth+sTemp;
end else
sOrderID:=sYear+sMonth+'0001';
end ;
result := sOrderID;
end;
//--------------------------------------------------------------------------//
class procedure TPublicFunction.AddItemToCombobox(fQuery:TQuery;fCombobox:Tcombobox;SqlStr:string);
begin
with fQuery do
begin
if active then close;
sql.Clear;
sql.Add(sqlStr);
open;
while not eof do
begin
fCombobox.Items.Add(Fields[0].asstring);
next;
end;
close;
end;
end;
//-----------------------------------------------------------------------------//
class procedure TPublicFunction.DeleteRecordWithDBTable(aDataSet:TdataSet);
begin
// 这里的 showbox 是调用的别的单元里的一个方法 ,考虑到要和现在的程序
// 在界面上保持一至,所以不用 messagebox 这里 用 messagebox 的方法给注
// 释起来了
if aDataSet.IsEmpty then exit;
if ShowBox('确定要删除当前这条记录吗?') then
begin
aDataSet.Delete;
try
aDataSet.Post;
except
end;
end;
{
if Messagebox(0,pchar('确定要删除当前这条记录吗?'),pchar('提示'),
windows.MB_OKCANCEL+MB_iconQuestion) = idOk then
begin
aDataSet.Delete;
try
aDataSet.Post;
except
end;
end;
}
end;
//-----------------------------------------------------------------------------//
Class procedure TPublicFunction.ClearTextFromPanel(thePanel:TPanel);
var
i:integer;
begin
for i:= 0 to thePanel.ControlCount-1 do
begin
if thePanel.Controls[i] is TEdit then
begin
TEdit(thePanel.Controls[i]).Text := ''
end;
end;
end;
//-----------------------------------------------------------------------------//
Class procedure TPublicFunction.CreateListViewColumn(theColumns:array of string;
theColumnWidth:array of integer;theListView:TListView);
var
i:integer;
aColumn:TListColumn;
begin
theListView.Columns.Clear;
theListView.ViewStyle := comctrls.vsReport;
for i := low(theColumns) to High(theColumns) do
begin
aColumn:=theListView.Columns.Add;
aColumn.Caption := theColumns[i];
aColumn.Width := theColumnWidth[i];
end;
end;
//-----------------------------------------------------------------------------//
Class procedure TPublicFunction.AddDataToListView(aDataSet:TDataset;
aListView:TListview;shCheckBox:boolean);
var
aItem : TListItem;
i: integer;
begin
aListView.Checkboxes := shCheckBox;
aListView.GridLines := true; // 显示网格线
aListView.Items.Clear;
aDataSet.First;
while not aDataset.Eof do
begin
aItem := aListView.Items.Add;
aItem.Caption := aDataSet.Fields[0].AsString;
for i:= 1 to aDataSet.FieldCount-1 do
begin
aItem.SubItems.Add(aDataSet.Fields[i].AsString);
end;
aDataSet.next;
end;
end;
end.
- 我在编程里用到的函数,归在一个类中(未完)
- ios-Xcode在编程中经常用到的快捷键
- 在VC++ IDE中一个很少用到的命令
- 在JS中一个function函数要调用另一个函数里的变量。
- C语言中,我想在一个函数里用另一个函数的变量怎么办?
- 在我的相册里
- C++里,我们通过在一个类中重载括号运算符的方法使用仿函数
- 在winform里怎么调用WebBrowser控件里的脚本 (转自思归呓语)
- ioctl函数在网络编程里的应用
- 一个我现在用到的Hibernate类
- 一个我现在用到的Hibernate类
- 在android中,经常用到的定时器
- 在使用SQL编程时需要用到的函数汇总,很有用。
- 在datalist中编程items里的控件
- MFC中,怎样在一个特定的控件里画图
- 在ASP中判断一个TEXT里输入的数据类型
- 在一个 application 中调用另一个 application 里的 Activity
- 8939 在Sbl里的pmic中使用延迟函数
- AIX相关命令
- java 常见考题
- C# String 与 string的问题
- 一些基础算法
- linux/freebsd下php连接ms sqlserver
- 我在编程里用到的函数,归在一个类中(未完)
- 更改WebSphere默认应用端口
- wirefusion中的交互1
- SQL-1
- myEclipse错误处理--An internal error occurred during: "Generating Artifacts".
- Alpha compositing & Porter-Duff model
- 面试题
- [心情]孤独,这种感觉不好受
- 驾驭“野马”-- 探索Java SE 6的一些新特性