Files
WorkNote/SanPinPLM/相关操作/4.0-other/999.Delphi脚本记录.md

741 lines
24 KiB
Markdown
Raw Normal View History

2025-05-07 19:33:34 +08:00
# 介绍
2025-03-04 13:22:52 +08:00
2025-03-04 13:24:39 +08:00
本笔记用于记录所有编制过的 `Delphi` 脚本,以便于沉淀相关知识
2025-03-04 13:37:23 +08:00
## 多属性值拼接至另一属性中
`fedtusr_substrate_id``fedtusr_substrate_id2``fedtusr_substrate_id3``fedtusr_substrate_id4` 控件中获取值,以逗号为分隔符,按顺序进行拼接,结果输出到 `mmmusr_substrate2` 控件中
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
StringList: TStringList;
begin
// 创建和初始化TStringList
StringList := TStringList.Create;
try
// 如果fedtusr_substrate_id的值不为空则将值添加到StringList中
if fedtusr_substrate_id.Text <> '' then
StringList.Add(fedtusr_substrate_id.Text);
if fedtusr_substrate_id2.Text <> '' then
StringList.Add(fedtusr_substrate_id2.Text);
if fedtusr_substrate_id3.Text <> '' then
StringList.Add(fedtusr_substrate_id3.Text);
if fedtusr_substrate_id4.Text <> '' then
StringList.Add(fedtusr_substrate_id4.Text);
// 转换为逗号分隔的字符串
mmmusr_substrate2.Text := StringList.DelimitedText; // 默认使用逗号作为分隔符
// 如果需要指定其他分隔符可以设置Delimiter属性
// StringList.Delimiter := ';'; // 设置分隔符为分号
// Result := StringList.DelimitedText;
finally
StringList.Free;
end;
end.
```
## 属性值添加至另一属性中
`fedtusr_gys` 控件中的值有多个,采取 `;` 进行分隔,现要验证 `fedtusr_shgys` 控件中的值是否存在于 `fedtusr_gys` 控件中,如果不存在,则将其添加进去,并进行排序
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
gysList: TStringList;
begin
try
// 初始化 TStringList
gysList := TStringList.Create;
gysList.Delimiter := ';';
gysList.StrictDelimiter := True;
// 将 fedtusr_gys 按分号分隔成列表
gysList.DelimitedText := fedtusr_gys.Text;
if gysList.IndexOf(fedtusr_shgys.Text) = -1 then
begin
// 如果不存在,则添加到 fedtusr_gys 中
gysList.Add(fedtusr_shgys.Text);
gysList.Sort;
// 将列表重新组合成字符串,使用分号分隔
fedtusr_gys.Text := gysList.DelimitedText;
end;
finally
gysList.Free;
end;
end.
```
## 属性值是否被另一属性所包含校验
2025-03-04 13:44:41 +08:00
`fedtusr_gys` 控件中的值有多个,采取 `;` 进行分隔,现要验证 `fedtusr_shgys` 控件中的值是否存在于 `fedtusr_gys` 控件中,如果不存在,则弹窗提示
```delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
gysList: TStringList;
begin
try
// 初始化 TStringList
gysList := TStringList.Create;
gysList.Delimiter := ';';
gysList.StrictDelimiter := True;
// 将 fedtusr_gys 按分号分隔成列表
gysList.DelimitedText := fedtusr_gys.Text;
if gysList.IndexOf(fedtusr_shgys.Text) = -1 then
begin
ShowMessage('默认供应商未包含在供应商列表中');
end;
finally
gysList.Free;
end;
end.
2025-03-11 13:34:52 +08:00
```
## 不同属性一致性设置
2025-03-12 15:04:04 +08:00
`fedtDrawId` 的值设置为 `fedtItemCode` 的值
2025-03-11 13:34:52 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
begin
//注释:在下面添加您的脚本代码
// 检查 物料编码 是否为空
if Trim(fedtItemCode.Text) <> '' then
begin
// 如果 物料编码 不为空,则将 代号 的值设置为 物料编码 的值
fedtDrawId.Text := fedtItemCode.Text;
end
else
begin
// 如果 物料编码 为空,则清空 代号
fedtDrawId.Text := '';
end;
end.
2025-05-07 19:48:34 +08:00
```
## 执行SQL语句获取查询结果
2025-05-07 19:50:35 +08:00
主要内容为通过 `chiGetFieldValueBySql` 方法去执行 ` SQL ` 语句,通过物料代码去获取指定属性的值,要求是这个物料得先被创建出来,才能正常执行脚本,否则会提示找不到这个物料
2025-05-07 19:48:34 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
Var
str: String;
begin
try
// 1. 查询usr_333的值
str := chiGetFieldValueBySql('select usr_333 from pdmitem where itemcode = ''' + fedtItemCode.Text + '''');
// 2. 检查查询结果
if str = '' then
begin
ShowMessage('未找到物料编码: ' + fedtItemCode.Text);
Exit;
end;
// 3. 如果usr_333为1则更新usr_444为1
if str = '1' then
begin
// 执行更新操作
cbxusr_444.checked := false;
// 可选:显示操作成功提示
ShowMessage('已自动将usr_444更新为1');
end;
except
on E: Exception do
ShowMessage('操作失败: ' + E.Message);
end;
end.
2025-05-07 19:57:34 +08:00
```
## 更新是否控件
2025-05-07 19:59:34 +08:00
检验 `cbxusr_333` 控件的值,如果为 `false`,那么 `cbxusr_444` 控件的值必须为 `false`
2025-05-07 19:57:34 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
begin
try
// 校验逻辑:当 cbxusr_333 未选中时,强制 cbxusr_444 也未选中
if not cbxusr_333.Checked then
begin
// 如果 cbxusr_444 当前是选中状态,则强制取消选中
if cbxusr_444.Checked then
begin
cbxusr_444.Checked := False;
// 可选:显示提示信息
ShowMessage('已自动将444设为未选中状态因为333未选中');
end;
end;
except
on E: Exception do
ShowMessage('执行脚本时出错: ' + E.Message);
end;
end.
2025-05-07 20:34:34 +08:00
```
2025-05-08 20:01:04 +08:00
## 批量校验控件并进行更新
2025-05-08 20:05:46 +08:00
自动检查并强制更新一组相关联的复选框(CheckBox)控件的状态,确保当某个"启用"复选框未被选中时,其相关联的"影响"复选框也必须处于未选中状态
2025-05-08 20:07:46 +08:00
**脚本核心功能**
2025-05-08 20:05:46 +08:00
- **强制关联逻辑**:当某个"启用"复选框(如cbxusr_01_FIsEnable)未被选中时自动将其关联的3个"影响"复选框(cbxusr_01_FIsAffectPrice、cbxusr_01_FIsAffectPlan、cbxusr_01_FIsAffectCost)设为未选中状态
- **适用范围**处理5组不同的控件组合(01-04和06),每组都有相同的关联关系
2025-05-08 20:07:46 +08:00
**控件命名规则与对应关系**
- **命名模式**`cbxusr_XX_FIsYYYYY`
- `XX`代表业务类型01(仓库)、02(仓位)、03(BOM版本)、04(批号)、06(计划跟踪号)
- `FIsYYYYY`代表功能:
- `FIsEnable`:启用控制
- `FIsAffectPrice`:影响价格
- `FIsAffectPlan`:影响计划
- `FIsAffectCost`:影响出库成本
2025-05-08 20:01:04 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
ModifiedControls: TStringList;
CurrentForm: TForm; // 添加窗体变量
procedure ProcessControls(EnablePrefix: string);
var
EnableCheckBox: TCheckBox;
AffectCheckBox: TCheckBox;
AffectNames: array[0..2] of string;
AffectControlName: string;
i: Integer;
begin
AffectNames[0] := 'FIsAffectPrice';
AffectNames[1] := 'FIsAffectPlan';
AffectNames[2] := 'FIsAffectCost';
// 通过当前窗体查找组件
EnableCheckBox := TCheckBox(CurrentForm.FindComponent('cbxusr_' + EnablePrefix + '_FIsEnable'));
if Assigned(EnableCheckBox) and not EnableCheckBox.Checked then
begin
for i := 0 to High(AffectNames) do
begin
AffectControlName := 'cbxusr_' + EnablePrefix + '_' + AffectNames[i];
AffectCheckBox := TCheckBox(CurrentForm.FindComponent(AffectControlName));
if Assigned(AffectCheckBox) and AffectCheckBox.Checked then
begin
ModifiedControls.Add(AffectControlName);
AffectCheckBox.Checked := False;
end;
end;
end;
end;
begin
ModifiedControls := TStringList.Create;
try
try
// 获取当前活动窗体
CurrentForm := Screen.ActiveForm;
if not Assigned(CurrentForm) then
begin
ShowMessage('无法获取当前窗体');
Exit;
end;
ProcessControls('01');
ProcessControls('02');
ProcessControls('03');
ProcessControls('04');
ProcessControls('06');
if ModifiedControls.Count > 0 then
begin
ShowMessage(
'以下控件的值已被自动修改为未选中状态:' + #13#10 +
'--------------------------------' + #13#10 +
ModifiedControls.Text + #13#10 +
'--------------------------------' + #13#10 +
'因为对应的Enable控件未选中'
);
end;
except
on E: Exception do
ShowMessage('执行脚本时出错: ' + E.Message);
end;
finally
ModifiedControls.Free;
end;
end.
```
2025-05-08 20:05:46 +08:00
### 进一步优化
2025-05-08 20:12:04 +08:00
**中文语义化输出(核心优化)**
- 新增 `GetControlChineseName` 函数实现控件名称到中文描述的智能转换
- 示例转换:
- `cbxusr_01_FIsEnable` → `启用(仓库)`
- `cbxusr_02_FIsAffectPrice` → `影响价格(仓位)`
- 输出信息从技术性控件名变为业务人员可读的自然语言
2025-05-08 20:05:46 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
ModifiedControls: TStringList;
CurrentForm: TForm;
// 获取控件的中文描述
function GetControlChineseName(ControlName: string): string;
var
Prefix, Suffix: string;
PrefixDesc, SuffixDesc: string;
begin
// 提取前缀(01/02/03/04/06)和后缀(FIsEnable/FIsAffectPrice等)
Prefix := Copy(ControlName, Pos('_', ControlName) + 1, 2);
Suffix := Copy(ControlName, Pos('_', ControlName) + 4, MaxInt);
// 映射前缀到中文描述
if Prefix = '01' then PrefixDesc := '仓库'
else if Prefix = '02' then PrefixDesc := '仓位'
else if Prefix = '03' then PrefixDesc := 'BOM版本'
else if Prefix = '04' then PrefixDesc := '批号'
else if Prefix = '06' then PrefixDesc := '计划跟踪号'
else PrefixDesc := '未知';
// 映射后缀到中文描述
if Suffix = 'FIsEnable' then SuffixDesc := '启用'
else if Suffix = 'FIsAffectPrice' then SuffixDesc := '影响价格'
else if Suffix = 'FIsAffectPlan' then SuffixDesc := '影响计划'
else if Suffix = 'FIsAffectCost' then SuffixDesc := '影响出库成本'
else SuffixDesc := '未知';
Result := SuffixDesc + '(' + PrefixDesc + ')';
end;
procedure ProcessControls(EnablePrefix: string);
var
EnableCheckBox: TCheckBox;
AffectCheckBox: TCheckBox;
AffectNames: array[0..2] of string;
AffectControlName, EnableControlName: string;
i: Integer;
ChineseDesc: string;
begin
AffectNames[0] := 'FIsAffectPrice';
AffectNames[1] := 'FIsAffectPlan';
AffectNames[2] := 'FIsAffectCost';
EnableControlName := 'cbxusr_' + EnablePrefix + '_FIsEnable';
EnableCheckBox := TCheckBox(CurrentForm.FindComponent(EnableControlName));
if Assigned(EnableCheckBox) and not EnableCheckBox.Checked then
begin
for i := 0 to High(AffectNames) do
begin
AffectControlName := 'cbxusr_' + EnablePrefix + '_' + AffectNames[i];
AffectCheckBox := TCheckBox(CurrentForm.FindComponent(AffectControlName));
if Assigned(AffectCheckBox) and AffectCheckBox.Checked then
begin
ChineseDesc := GetControlChineseName(AffectControlName);
ModifiedControls.Add(ChineseDesc + ' 已被自动修改为未选中状态,因为 ' +
GetControlChineseName(EnableControlName) + ' 未被选中');
AffectCheckBox.Checked := False;
end;
end;
end;
end;
begin
ModifiedControls := TStringList.Create;
try
try
CurrentForm := Screen.ActiveForm;
if not Assigned(CurrentForm) then
begin
ShowMessage('无法获取当前窗体');
Exit;
end;
ProcessControls('01');
ProcessControls('02');
ProcessControls('03');
ProcessControls('04');
ProcessControls('06');
if ModifiedControls.Count > 0 then
begin
ShowMessage(
'以下控件的值已被自动修改:' + #13#10 +
'--------------------------------' + #13#10 +
ModifiedControls.Text + #13#10 +
'--------------------------------'
);
end;
except
on E: Exception do
ShowMessage('执行脚本时出错: ' + E.Message);
end;
finally
ModifiedControls.Free;
end;
end.
```
2025-05-07 20:34:34 +08:00
## 更新枚举选项
2025-07-23 12:50:24 +08:00
`cbxusr_444` 枚举控件的值必须等于 `cbxusr_333` 枚举控件的值,需要使用到 `itemindex` 去获取
2025-05-07 20:36:34 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
begin
2025-05-16 15:44:07 +08:00
cbxusr_444.itemindex := cbxusr_333.itemindex;
2025-05-07 20:36:34 +08:00
end.
2025-05-16 15:44:07 +08:00
```
2025-07-23 12:50:24 +08:00
### 枚举itemindex说明
2025-07-23 12:52:24 +08:00
`itemindex` 是指枚举的序号,当为空时,`itemindex``-1`,不为空时,从 `1` 开始计数(切记!不是从 `0` 开始)
如果枚举序号不同,哪怕枚举值一样也是无用的
2025-07-23 12:54:24 +08:00
> 例如
>
> `cbxusr_444` 枚举控件有三个枚举分别是:`A`、`B`、`C`,对应的值是:`1``2``3`
>
> `cbxusr_333` 枚举控件有三个枚举分别是:`3`、`2`、`1`,对应的值是:`C``B``A`
>
> 若想达到效果,当 `cbxusr_444` 枚举控件选择 `A` 的时候,`cbxusr_333` 枚举控件要更新成 `3`
>
2025-07-23 12:58:24 +08:00
> 那么使用 `cbxusr_333.itemindex := cbxusr_444.itemindex;` 是无法实现的,因为 `cbxusr_444` 枚举控件选择 `A` 的时候,`iteminde` 为 `1`,而 `cbxusr_333` 枚举控件的 `itemindex` 为 `1` 的时候,对应的是枚举 `3`
2025-07-23 12:50:24 +08:00
2025-05-17 09:06:33 +08:00
## 枚举项联动
2025-05-20 09:27:12 +08:00
`fedtusr_FBaseUnitId` 控件有值,且 `fedtusr_FPurchaseUnitId``fedtusr_FSaleUnitId``fedtusr_FSalePriceUnitId` 等控件没有选择值的情况下,将 `fedtusr_FBaseUnitId` 控件的值赋予给其他控件(仅限于 **值改变时执行**
2025-05-17 11:21:17 +08:00
2025-05-20 11:12:59 +08:00
枚举控件空值为-1从0开始计数
2025-05-17 11:21:17 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
begin
//注释:在下面添加您的脚本代码
if (fedtusr_FBaseUnitId.itemindex <> -1) and (fedtusr_FPurchaseUnitId.Text = '') then
fedtusr_FPurchaseUnitId.itemindex := fedtusr_FBaseUnitId.itemindex;
if (fedtusr_FBaseUnitId.itemindex <> -1) and (fedtusr_FPurchasePriceUnitId.Text = '') then
fedtusr_FPurchasePriceUnitId.itemindex := fedtusr_FBaseUnitId.itemindex;
if (fedtusr_FBaseUnitId.itemindex <> -1) and (fedtusr_FSaleUnitId.Text = '') then
fedtusr_FSaleUnitId.itemindex := fedtusr_FBaseUnitId.itemindex;
if (fedtusr_FBaseUnitId.itemindex <> -1) and (fedtusr_FSalePriceUnitId.Text = '') then
fedtusr_FSalePriceUnitId.itemindex := fedtusr_FBaseUnitId.itemindex;
if (fedtusr_FBaseUnitId.itemindex <> -1) and (fedtusr_FStoreUnitID.Text = '') then
fedtusr_FStoreUnitID.itemindex := fedtusr_FBaseUnitId.itemindex;
if (fedtusr_FBaseUnitId.itemindex <> -1) and (fedtusr_FMinIssueUnitId.Text = '') then
fedtusr_FMinIssueUnitId.itemindex := fedtusr_FBaseUnitId.itemindex;
end.
2025-05-19 09:16:21 +08:00
```
## 获取枚举值写入到文本中
获取枚举控件A的值写入到文本控件B中不适用于装配属性
2025-05-17 11:21:17 +08:00
2025-05-19 09:16:21 +08:00
```Delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
begin
if A.itemindex <> -1 then
B.Text := A.Text;
end.
2025-05-20 11:14:59 +08:00
```
## 截取指定字符前的字符串
`cbxusr_pinpai` 未被勾选时,只保留 `fedtItemCode``-` 之前的部分
```Delphi
uses
MyClass, Variables, BaseUtil, CommonFunc, DataConst, CFFrm, CFSimplePropFrm,
Forms, StdCtrls, Variants, SysUtils, Classes, Controls, Dialogs,
CHostIntf, ProductClas, DocClas, LoginClas, VirtualTrees, CEntClas, PathClas;
var
DashPos: Integer; // 存储 "-" 的位置
begin
// 检查条件itemcode 包含 "-" 并且 cbxusr_pinpai 未勾选
if (Pos('-', fedtItemCode.Text) > 0) and (cbxusr_pinpai.Checked = false) then
begin
// 找到 "-" 的位置
DashPos := Pos('-', fedtItemCode.Text);
// 只保留 "-" 之前的部分
fedtItemCode.Text := Copy(fedtItemCode.Text, 1, DashPos - 1);
end;
if cbxusr_pinpai.checked = false then
fedtusr_gongyingshang.itemindex := -1;
end.
2025-06-16 12:28:37 +08:00
```
2025-06-18 15:52:30 +08:00
## 转换字符串数组的连接字符
2025-06-16 12:28:37 +08:00
2025-06-18 15:52:30 +08:00
当要设置自定义分隔符号,`StringList.Delimiter := '-';` 需要写在转化语句之前
2025-06-16 12:30:37 +08:00
2025-07-14 10:52:43 +08:00
<<<<<<< HEAD
2025-06-16 12:30:37 +08:00
2025-06-20 13:58:10 +08:00
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
2025-06-16 12:30:37 +08:00
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
2025-07-14 10:52:43 +08:00
=======
2025-06-19 11:06:50 +08:00
```delphi
2025-06-19 11:08:50 +08:00
uses
MyClass, Variables, BaseUtil, CommonFunc, DataConst, CFFrm, CFSimplePropFrm,
Forms, StdCtrls, Variants, SysUtils, Classes, Controls, Dialogs,
CHostIntf, ProductClas, DocClas, LoginClas, VirtualTrees, CEntClas, PathClas;
2025-07-14 10:52:43 +08:00
>>>>>>> origin/main
2025-06-16 12:30:37 +08:00
var
StringList: TStringList;
begin
// 创建和初始化TStringList
StringList := TStringList.Create;
try
if fedtusr_KHMC.Text <> '' then
StringList.Add(fedtusr_KHMC.Text);
if fedtusr_khth.Text <> '' then
StringList.Add(fedtusr_khth.Text);
if fedtusr_lb.Text <> '' then
StringList.Add(fedtusr_lb.Text);
2025-06-18 15:52:30 +08:00
StringList.Delimiter := '-'; // 设置分隔符为分号
2025-06-16 12:30:37 +08:00
// 转换为逗号分隔的字符串
fedtFShtName.Text := StringList.DelimitedText; // 默认使用逗号作为分隔符
finally
StringList.Free;
end;
2025-06-19 11:06:50 +08:00
end.
```
2025-06-19 11:08:50 +08:00
### 拼接的另一种方式
2025-06-19 11:10:50 +08:00
由于采用 `StringList` 拼接会出现拼接的字符串中包含特殊字符时,默认给该字符串加上双引号的的情况,如图所示
![](assets/0875f15131f11e246ef28751293de75f.png)
那么对此可以采用另一种拼接方式,直接将字符串与字符串拼接起来,不再使用 `StringList` 记录再转化
```Delphi
2025-06-20 09:59:44 +08:00
uses
MyClass, Variables, BaseUtil, CommonFunc, DataConst, CFFrm, CFSimplePropFrm,
Forms, StdCtrls, Variants, SysUtils, Classes, Controls, Dialogs,
CHostIntf, ProductClas, DocClas, LoginClas, VirtualTrees, CEntClas, PathClas;
2025-06-19 11:10:50 +08:00
var
TempStr: string;
begin
TempStr := '';
if fedtusr_KHMC.Text <> '' then
TempStr := fedtusr_KHMC.Text;
if fedtusr_khth.Text <> '' then
begin
if TempStr <> '' then
TempStr := TempStr + '-'; // 添加分隔符
TempStr := TempStr + fedtusr_khth.Text;
end;
if fedtusr_lb.Text <> '' then
begin
if TempStr <> '' then
TempStr := TempStr + '-'; // 添加分隔符
TempStr := TempStr + fedtusr_lb.Text;
end;
fedtFShtName.Text := TempStr; // 直接赋值
end.
2025-07-09 15:36:38 +08:00
```
## 删除文本中的空格
2025-07-09 15:42:39 +08:00
删除掉指定控件中输入的文本的空格
2025-07-09 15:44:39 +08:00
```delphi
uses
MyClass, Variables, BaseUtil, CommonFunc, DataConst, CFFrm, CFSimplePropFrm,
Forms, StdCtrls, Variants, SysUtils, Classes, Controls, Dialogs,
CHostIntf, ProductClas, DocClas, LoginClas, VirtualTrees, CEntClas, PathClas;
// 在begin之前定义过程
procedure RemoveSpacesFromEdit(EditControl: TCustomEdit);
var
OriginalText: string;
begin
OriginalText := EditControl.Text;
// 使用StringReplace函数删除所有空格
EditControl.Text := StringReplace(OriginalText, ' ', '', [rfReplaceAll]);
end;
begin
// 调用过程处理edtSpec控件
RemoveSpacesFromEdit(edtSpec);
end.
```
### 删除其他空白字符
2025-07-09 15:48:43 +08:00
如果需要删除其他空白字符(如制表符、换行符),可以修改过程为
2025-07-09 15:44:39 +08:00
2025-07-09 15:46:43 +08:00
```delphi
uses
MyClass, Variables, BaseUtil, CommonFunc, DataConst, CFFrm, CFSimplePropFrm,
Forms, StdCtrls, Variants, SysUtils, Classes, Controls, Dialogs,
CHostIntf, ProductClas, DocClas, LoginClas, VirtualTrees, CEntClas, PathClas;
2025-07-09 15:44:39 +08:00
2025-07-09 15:46:43 +08:00
// 在begin之前定义过程
procedure RemoveSpacesFromEdit(EditControl: TCustomEdit);
var
OriginalText: string;
begin
OriginalText := EditControl.Text;
// 删除所有空白字符
OriginalText := StringReplace(OriginalText, ' ', '', [rfReplaceAll]);
// 制表符
OriginalText := StringReplace(OriginalText, #9, '', [rfReplaceAll]);
// 换行符
OriginalText := StringReplace(OriginalText, #13#10, '', [rfReplaceAll]);
EditControl.Text := OriginalText;
end;
begin
// 调用过程处理edtSpec控件
RemoveSpacesFromEdit(edtSpec);
end.
2025-07-10 10:19:33 +08:00
```
2025-07-10 10:23:33 +08:00
## 根据输入内容在数据库中查询,获取查询结果作为下拉列表以供选择
2025-07-10 10:40:55 +08:00
根据在 `fedtusr_sqlmj` 控件中输入的内容,在数据库中进行查询,并将查询结果去重后,列出头十条作为下拉选择,以供 `fedtusr_sqlmj` 控件选择
2025-07-10 10:38:55 +08:00
2025-07-10 10:36:55 +08:00
```delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
str:string;
begin
//注释:在下面添加您的脚本代码
str := fedtusr_sqlmj.Text;
if Length(str) > 0 then
begin
if Length(chiGetFieldValueBySql('select top 1 usr_sqlmj from pdmitem where usr_sqlmj like ''%' + str + '%''')) > 0 then
begin
2025-07-10 10:38:55 +08:00
chiFillComboBox('select distinct top 10 usr_sqlmj from pdmitem where usr_sqlmj like ''%' + str + '%''',fedtusr_sqlmj);
2025-07-10 10:36:55 +08:00
end
else
begin
2025-07-10 10:38:55 +08:00
chiFillComboBox('select ''''',fedtusr_sqlmj);
2025-07-10 10:36:55 +08:00
end;
end;
end.
2025-07-10 10:40:55 +08:00
```
2025-07-10 10:43:30 +08:00
### 前置要求
2025-07-10 10:51:30 +08:00
接收下拉选择的控件的 **按钮类型** 必须是 **下拉** 才行(属性的类型可以是文本,也可以是枚举,枚举要求枚举来源为空)
2025-07-10 10:43:30 +08:00
2025-07-10 10:45:30 +08:00
![](assets/Pasted%20image%2020250710104426.png)
2025-07-10 10:47:30 +08:00
此外,脚本必须放在 **按钮****单击执行**
2025-07-22 10:39:09 +08:00
![](assets/Pasted%20image%2020250710104559.png)
## 获取当前日期拼接到字符串中
获取当前的日期,并以 `yyyy-mm-dd` 的格式拼接到字符串末尾
```delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
TempStr: string;
begin
TempStr := '';
if fedtusr_lb.Text <> '' then
begin
TempStr := TempStr + fedtusr_lb.Text;
end;
if fedtusr_KHMC.Text <> '' then
begin
if TempStr <> '' then
TempStr := TempStr + '-'; // 添加分隔符
TempStr := TempStr + fedtusr_KHMC.Text;
end;
if fedtusr_khth.Text <> '' then
begin
if TempStr <> '' then
TempStr := TempStr + '-'; // 添加分隔符
TempStr := TempStr + fedtusr_khth.Text;
end;
// 替换为当前时间(总是添加)
if TempStr <> '' then
TempStr := TempStr + '-'; // 添加分隔符
TempStr := TempStr + FormatDateTime('yyyy-mm-dd', Now); // 使用日期格式
fedtFShtName.Text := TempStr; // 直接赋值
end.
2025-07-23 13:29:35 +08:00
```
## 多选枚举控件同步
2025-07-23 13:56:11 +08:00
多选枚举同步,需要在属性保存下来后,通过同步按钮进行同步(读写数据库进行)
2025-07-23 13:29:35 +08:00
```delphi
uses MyClass,Variables,BaseUtil,CommonFunc,DataConst,CFFrm,CFSimplePropFrm,Forms,StdCtrls,Variants,SysUtils,Classes,Controls,Dialogs,
CHostIntf,ProductClas,DocClas,LoginClas,VirtualTrees,CEntClas,PathClas;
var
str:string;
begin
//注释:在下面添加您的脚本代码
if fedtusr_mj1.Text <> '' then
begin // 添加begin
str := chiGetFieldValueBySql('select usr_mj1 from pdmitem where itemcode = ' + QuotedStr(fedtItemCode.Text));
chiExecuteSqlNoResultRec('update pdmitem set usr_mj2 = ' + QuotedStr(str) + ' where itemcode = ' + QuotedStr(fedtItemCode.Text));
end; // 添加end
end.
2025-07-22 10:39:09 +08:00
```