为TDBLookupComboboxEh增加自定义列宽,用于设置下拉框中各列的宽度,而避免必须用DropDownBox才能设置的问题。
TCustomDBLookupComboboxEh = class(TCustomDBEditEh, ILookupGridOwner)
Private
FListWidths: string;
procedure SetListWidths(const Value: string);
public
property ListWidths: string read FListWidths write SetListWidths;
end;
// 源字符串按照分隔符分割成TstringList,返回生成的行数
function StringToList(AList: TStrings; AString,
ASeparator: string;AClear: Boolean): Integer;
var
cStr : string;
i : Integer;
begin
Result := 0;
if AClear then AList.Clear;
if AString = '' then Exit;
cStr := AString;
i := Pos(ASeparator,cStr);
while i <> 0 do
begin
AList.Add(Copy(cStr,0,i -1));
Delete(cStr,1,i);
i := Pos(ASeparator,cStr);
end;
AList.Add(cStr);
Result := AList.Count;
end;
function CountCharInStr(const Sub: Char; const AStr: string): Integer;
var
I: Integer;
begin
Result := 0;
if AStr = '' then Exit;
for I := 1 to Length(AStr) do
if AStr[I] = Sub then
Inc(Result);
end;
procedure TCustomDBLookupComboboxEh.SetListWidths(const Value: string);
var
lStr1,lStr2: TStrings;
i,iLen,iSum: Integer;
begin
// FDataList.Columns.Add
//设置下拉列表框的字段宽度
if FListWidths <> Value then
begin
FListWidths := Value;
FDataList.Columns.Clear;
//FDropDownBox.Columns.Clear;
//FListFieldName 与 FListWidths 中的节数相同
if (FListFieldName <> '') and (CountCharInStr(';',FListFieldName) = CountCharInStr(';',FListWidths)) then
begin
lStr1 := StringToList(FListFieldName,';');
lStr2 := StringToList(FListWidths,';');
iSum := 0;
for i := 0 to lStr2.Count - 1 do
begin
if (lStr1[i] = '') then Continue;
iLen := StrToIntDef(lStr2[i],100);
iSum := iSum + iLen;
//with FDropDownBox.Columns.Add do
with FDataList.Columns.Add do
begin
FieldName := lStr1[i];
Width := iLen;
end;
end;
FDropDownBox.Width := iSum;
lStr1.Free;
lStr2.Free;
end;
end;
end;