首页  编辑  

字符网格和文本间转换

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

(*//

标题:字符网格和文本间转换

说明:StringGrid

设计:Zswang

日期:2002-02-21

支持:wjhu111@21cn.com

//*)

///////Begin Source

uses

 Math;

function StrLeft(const mStr: string; mDelimiter: string): string;

begin

 Result := Copy(mStr, 1, Pos(mDelimiter, mStr) - 1);

end; { StrLeft }

function ListValue(mList: string; mIndex: Integer; mDelimiter: string = ','): string;

var

 I, L, K: Integer;

begin

 L := Length(mList);

 I := Pos(mDelimiter, mList);

 K := 0;

 Result := '';

 while (I > 0) and (K <> mIndex) do begin

   mList := Copy(mList, I + Length(mDelimiter), L);

   I := Pos(mDelimiter, mList);

   Inc(K);

 end;

 if K = mIndex then Result := StrLeft(mList + mDelimiter, mDelimiter);

end; { ListValue }

function StringGridToText(mStringGrid: TStringGrid;

 mStrings: TStrings): Boolean;

var

 I, J: Integer;

 T: string;

begin

 Result := False;

 if (not Assigned(mStringGrid)) or (not Assigned(mStrings)) then Exit;

 with mStringGrid do try

   mStrings.Clear;

   for J := 0 to RowCount - 1 do begin

     T := '';

     for I := 0 to ColCount - 1 do

       T := T + #9 + Cells[I, J];

     Delete(T, 1, 1);

     mStrings.Add(T);

   end;

 except

   Exit;

 end;

 Result := True;

end; { StringGridToText }

function TextToStringGrid(mStrings: TStrings;

 mStringGrid: TStringGrid): Boolean;

var

 I, J: Integer;

 T: string;

begin

 Result := False;

 if (not Assigned(mStringGrid)) or (not Assigned(mStrings)) then Exit;

 with mStrings, mStringGrid do try

   for I := 0 to ColCount - 1 do begin

     T := '';

     for J := 0 to Min(RowCount - 1, Count - 1) do

       Cells[I, J] := ListValue(Strings[J], I, #9);

   end;

 except

   Exit;

 end;

 Result := True;

end; { TextToStringGrid }

///////End Source

///////Begin Demo

procedure TForm1.Button1Click(Sender: TObject);

begin

 StringGridToText(StringGrid1, Memo1.Lines);

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

 TextToStringGrid(Memo1.Lines, StringGrid1);

end;

///////End Demo