關於變數的問題 |
答題得分者是:alvin26
|
liannfs
一般會員 發表:3 回覆:6 積分:1 註冊:2007-12-04 發送簡訊給我 |
|
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
Try It !
[code delphi] unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public seat_location: string; end; var Form1: TForm1; implementation uses Unit2; {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin seat_location:='A1'; Form2.ShowModal end; end. {***************************************************************************} unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm2 = class(TForm) procedure FormClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form2: TForm2; implementation uses Unit1; {$R *.dfm} procedure TForm2.FormClick(Sender: TObject); begin CAPTION:='GET FORM1 ' FORM1.seat_location; end; end. [/code]
------
What do we live for if not to make life less difficult for each other? |
liannfs
一般會員 發表:3 回覆:6 積分:1 註冊:2007-12-04 發送簡訊給我 |
|
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
|
liannfs
一般會員 發表:3 回覆:6 積分:1 註冊:2007-12-04 發送簡訊給我 |
~這是副表單~
[code delphi] unit order_1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, Calendar, ComCtrls; type Torder = class(TForm) Button1: TButton; Button2: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; Edit2: TEdit; Label4: TLabel; ComboBox1: TComboBox; Edit3: TEdit; Label5: TLabel; Label6: TLabel; DateTimePicker1: TDateTimePicker; Label7: TLabel; RadioButton1: TRadioButton; RadioButton2: TRadioButton; Label8: TLabel; seat_name: TLabel; Label9: TLabel; procedure Button2Click(Sender: TObject); procedure RadioButton1Click(Sender: TObject); procedure RadioButton2Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var order: Torder; implementation {$R *.dfm} uses main; procedure Torder.Button2Click(Sender: TObject); begin order.Hide; Form1.Show; end; procedure Torder.RadioButton1Click(Sender: TObject); begin DateTimePicker1.Visible:=True end; procedure Torder.RadioButton2Click(Sender: TObject); begin DateTimePicker1.Visible:=False; end; procedure Torder.FormCreate(Sender: TObject); begin Label9.Caption:=Form1.seat_location; end; end. [/code] ~而這是主表單~ [code delphi] unit main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, XPMan, ExtCtrls; type TForm1 = class(TForm) GroupBox1: TGroupBox; CheckBox1: TCheckBox; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Label9: TLabel; Label10: TLabel; Label11: TLabel; Label12: TLabel; Label13: TLabel; Label14: TLabel; Label15: TLabel; Label16: TLabel; Label17: TLabel; Label18: TLabel; Label19: TLabel; Label20: TLabel; Label21: TLabel; Edit1: TEdit; Label22: TLabel; Button1: TButton; Edit2: TEdit; Label24: TLabel; Button2: TButton; GroupBox2: TGroupBox; GroupBox3: TGroupBox; Label23: TLabel; Edit3: TEdit; Button3: TButton; Label25: TLabel; XPManifest1: TXPManifest; Label26: TLabel; Timer1: TTimer; Button4: TButton; Button5: TButton; Label27: TLabel; procedure Timer1Timer(Sender: TObject); procedure Button4Click(Sender: TObject); procedure CheckBox1Click(Sender: TObject); private { Private declarations } public seat_location: string; { Public declarations } end; type order_file=record cust_name:string; cust_id:string; price:string; order_date:string; ticket_num:integer; preserved:string; seat_code:string; flight_number:string; end; var Form1: TForm1; seat_location: string; order_a: array[1..52] of order_file; implementation {$R *.dfm} uses order_1; procedure TForm1.Timer1Timer(Sender: TObject); begin Label26.Caption:=DateTimeToStr(now); end; procedure TForm1.Button4Click(Sender: TObject); begin Form1.close(); end; procedure TForm1.CheckBox1Click(Sender: TObject); begin seat_location:='A1'; order.Show; end; end. [/code] 目的是想要讓副表單在FormCreate的時候就可以存取到主表單的變數seat_location ,麻煩了@@"
編輯記錄
liannfs 重新編輯於 2007-12-05 18:15:36, 註解 無‧
|
alvin26
初階會員 發表:24 回覆:41 積分:28 註冊:2005-04-12 發送簡訊給我 |
我猜想,在您專案中 Form1 和 Order 可能在啟動的時候就已經 Create 起來了。
您可以看看 Project > Options 裡的 Forms 頁籤, 是不是 Form1 和 Order 都存在左邊的 "Auto-create forms" 呢? 如果是的話,那麼在程式啟動時, 就會依 "Auto-create forms" 中的順序執行各自的 Create 了。 之後就不會再執行 Create 這個程序。 Order.Show 會觸動的是 FormShow 和 FormActivate 這兩個程序, 所以 seat_location 的值就沒有機會傳給 Order 了, 您可以試試把 Label9.Caption:=Form1.seat_location; 改放到 FormShow 中,這樣應該就可以了。 不知道這是不是您要的呢? 不是的話請見諒。
------
弟子規,聖人訓,首孝悌,次謹信,泛愛眾,而親仁,有餘力,則學文。 |
liannfs
一般會員 發表:3 回覆:6 積分:1 註冊:2007-12-04 發送簡訊給我 |
|
christie
資深會員 發表:30 回覆:299 積分:475 註冊:2005-03-25 發送簡訊給我 |
在 Project > Options 裡的 Forms 頁籤,
把 Order 移到右邊 Available forms。 試試^_^ procedure TForm1.CheckBox1Click(Sender: TObject); begin seat_location:='A1'; Order:=TOrder.Create(nil); Order.ShowModal; Order.Free; end;
------
What do we live for if not to make life less difficult for each other? |
liannfs
一般會員 發表:3 回覆:6 積分:1 註冊:2007-12-04 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |