首页  编辑  

网友zswang提供的几个函数

Tags: /超级猛料/Friends.网友专栏/zswang/   Date Created:

zswang(zs) (2001-4-23 14:52:00)  得0分

//我回送你几个我的宝宝函数

procedure ZsDataSetToTreeView(mDataSet: TDataSet; mTreeView: TTreeView);

{ 将数据源转换成可视树 }

type

 TArrayTreeNode = array of TTreeNode;

var

 vFieldCount: Integer;

 vStringList: TStringList;

 procedure pZsDataToTreeNode(mArrayTreeNode: TArrayTreeNode;

   mIndex: Integer; mValueList: string);

 var

   vTreeNode, vTempTreeNode: TTreeNode;

   vStr, S, vTemp: string;

   I: Integer;

   vArrayTreeNode: TArrayTreeNode;

 begin

   vTemp := mValueList;

   vStringList.Text := mValueList;

   vStr := #13#10;

   vTreeNode := nil;

   if mIndex < vStringList.Count then begin

     if (Pred(mIndex) >= Low(mArrayTreeNode)) and (Pred(mIndex) < High(mArrayTreeNode))then

       vTreeNode := mArrayTreeNode[Pred(mIndex)];

     vStr := vStringList[mIndex];

   end;

   vArrayTreeNode := mArrayTreeNode;

   with mDataSet do while not Eof do begin

     S := FieldByName(FieldDefs[mIndex].Name).AsString;

     if (vStr = #13#10) or (vStr <> S) or (mIndex = Pred(vFieldCount)) then

     begin

       vTreeNode := mTreeView.Items.AddChild(vTreeNode,

         FieldByName(FieldDefs[mIndex].Name).AsString);

       vStr := S;

       vTempTreeNode := vTreeNode;

       SetLength(vArrayTreeNode, vFieldCount);

       vArrayTreeNode[mIndex] := vTempTreeNode;

       for I := Succ(mIndex) to Pred(vFieldCount) do begin

         vTempTreeNode := mTreeView.Items.AddChild(vTempTreeNode,

           FieldByName(FieldDefs[I].Name).AsString);

         vArrayTreeNode[I] := vTempTreeNode;

       end;

       vStringList.Clear;

       for I := 0 to Pred(vFieldCount) do

         vStringList.Add(FieldByName(FieldDefs[I].Name).AsString);

       vTemp := vStringList.Text;

       Next;

       pZsDataToTreeNode(vArrayTreeNode, 0, vTemp);

       vArrayTreeNode := nil;

     end else begin

       pZsDataToTreeNode(vArrayTreeNode, Succ(mIndex), vTemp);

     end;

   end;

 end;

begin

 vStringList := TStringList.Create;

 mTreeView.Items.Clear;

 vFieldCount := mDataSet.FieldCount;

 mDataSet.First;

 pZsDataToTreeNode(nil, 0, '');

 vStringList.Free;

end; { ZsDataSetToTreeView }

////////放在TForm1.ComboBox1KeyUp用

function ZsComboBoxVisualInput(mComboBox: TComboBox;

 mCase: Boolean = True): Boolean; { ComboBox可视化输入 }

var

 I, T: Integer;

begin

 Result := False;

 if Assigned(mComboBox) then

   with mComboBox do for I := 0 to (Items.Count) do

     if (Pos(Text, Items[I]) = 1) and (Text <> Items[I]) then

     begin

       T := Length(Text);

       Text := Items[I];

       SelStart := T;

       SelLength := Length(Items[I]) - T;

       Result := True;

       Break;

     end

     else if not (mCase) and (Pos(UpperCase(Text), UpperCase(Items[I])) = 1) and

       (UpperCase(Text) <> UpperCase(Items[I])) then

     begin

       T := Length(Text);

       Text := Items[I];

       SelStart := T;

       SelLength := Length(Items[I]) - T;

       Result := True;

       Break;

     end

end; { ZsComboBoxVisualInput }

///////比如:VariantToText(Table1['Field1;Field2']);

function VariantToText(mVariant: Variant; mDivision: string = ','): string; { 将变体类型转换成文本返回 }

var

 vVarType: Integer;

 vVarLow: Integer;

 vVarHigh: Integer;

 I: Integer;

begin

 Result := '';

 vVarType := VarType(mVariant);

 case vVarType of

   varSmallint .. varString: AppendStr(Result, mDivision + VarToStr(mVariant));

 else if vVarType and (varArray or varVariant) <> 0 then

   begin

     vVarLow := VarArrayLowBound(mVariant, VarArrayDimCount(mVariant));

     vVarHigh := VarArrayHighBound(mVariant, VarArrayDimCount(mVariant));

     for I := vVarLow to vVarHigh do

       AppendStr(Result, mDivision + VariantToText(mVariant[I]));

   end else AppendStr(Result, mDivision + VarToStr(mVariant));

 end;

 Delete(Result, 1, Length(mDivision));

end; { VariantToText }

///////没有什么好说的

function TextToVariant(mText: string; mDivision: string = ','): Variant; { VariantToText反函数 }

var

 vLeft, vRight: string;

 I, L, T, vCount: Integer;

begin

 Result := Null;

 vCount := 0;

 vRight := mText;

 I := Pos(mDivision, vRight);

 L := Length(mDivision);

 T := Length(mText);

 while I > 0 do begin

   vLeft := Copy(vRight, 1, I - 1);

   vRight := Copy(vRight, I + L, T);

   I := Pos(mDivision, vRight);

   Inc(vCount);

 end;

 if vRight <> '' then Inc(vCount);

 case vCount of

   0: ;

   1: Result := mText;

 else

   Result := VarArrayCreate([0, Pred(vCount)], varVariant);

   vRight := mText;

   I := Pos(mDivision, vRight);

   vCount := 0;

   while I > 0 do begin

     vLeft := Copy(vRight, 1, I - 1);

     vRight := Copy(vRight, I + L, T);

     Result[vCount] := vLeft;

     I := Pos(mDivision, vRight);

     Inc(vCount);

   end;

   if vRight <> '' then Result[vCount] := vRight;

 end;

end; { TextToVariant }