線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1857
推到 Plurk!
推到 Facebook!

ComponentToString & StringToComponent 無作用??

答題得分者是:hagar
jeffreck
高階會員


發表:247
回覆:340
積分:197
註冊:2003-01-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-07-13 13:54:04 IP:61.218.xxx.xxx 未訂閱
請教前輩!!
ComponentToString & StringToComponent 無作用??    使用ComponentToString把Button轉一字串,再傳回來但沒作用??
 strlist := ComponentToString(self.Button1);
   self.Button2 :=   StringToComponent(strlist) as TButton ;
不知那錯了???    以下是我的程式碼,謝謝
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls, DBCtrls;    type
  TForm1 = class(TForm)
    DBListBox1: TDBListBox;
    DBListBox2: TDBListBox;
    Shape1: TShape;
    BitBtn1: TBitBtn;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure BitBtn1Click(Sender: TObject);      private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    uses Unit2;        {$R *.dfm}    function ComponentToString(Component: TComponent): string;    var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryStream.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStream.Free;        end;
  finally
    BinStream.Free
  end;
end;    function StringToComponent(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result := BinStream.ReadComponent(nil);        finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
var
strlist : string;
begin
 strlist := ComponentToString(self.Button1);
   self.Button2 :=   StringToComponent(strlist) as TButton ;    end;
initialization        //add
RegisterClasses([TButton]);    end.    謝謝各位前輩!!  
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-07-14 08:23:23 IP:202.39.xxx.xxx 未訂閱
改成如下試試:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  strlist: string;
begin
  strlist := ComponentToString(self.Button1);      Self.Button2.Free;
  Self.Button2 := nil;      Self.Button2 := StringToComponent(strlist) as TButton;
  Self.Button2.Parent := Self; // 指定 Parent 才能顯示
  Button2.Left := Button1.Left   Button1.Width; // 改變一下 Left 屬性,不然它是與 Button1 重疊在一起的
end;
-- 向 KTop 的弟兄們致敬!
jeffreck
高階會員


發表:247
回覆:340
積分:197
註冊:2003-01-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-07-14 11:19:54 IP:61.218.xxx.xxx 未訂閱
引言: 改成如下試試:
procedure TForm1.BitBtn1Click(Sender: TObject);
var
  strlist: string;
begin
  strlist := ComponentToString(self.Button1);      Self.Button2.Free;
  Self.Button2 := nil;      Self.Button2 := StringToComponent(strlist) as TButton;
  Self.Button2.Parent := Self; // 指定 Parent 才能顯示
  Button2.Left := Button1.Left + Button1.Width; // 改變一下 Left 屬性,不然它是與 Button1 重疊在一起的
end;
謝謝前輩的回覆,這樣就可以了 但還有一個奇怪的問題?? 就是當物件為ListBox是,可以正常轉換 但如 ListBox的Items 有資料時就會錯誤 我試TreeView 也有相同情況?? 謝謝前輩!! 發表人 - jeffreck 於 2004/07/14 11:40:47
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-07-14 13:30:45 IP:202.39.xxx.xxx 未訂閱
參考這兩篇當中 Peter Below 的回答部份: http://groups.google.com/groups?hl=zh-TW&lr=&ie=UTF-8&th=c8317e13e326946c&rnum=1 http://groups.google.com/groups?hl=zh-TW&lr=&ie=UTF-8&th=40d6bba30270277b&rnum=2    -- 向 KTop 的弟兄們致敬! 
系統時間:2024-05-18 15:52:12
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!