全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1764
推到 Plurk!
推到 Facebook!

關於變數的問題

答題得分者是:alvin26
liannfs
一般會員


發表:3
回覆:6
積分:1
註冊:2007-12-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-12-04 08:58:33 IP:61.31.xxx.xxx 訂閱
各位好,我想請問的是,現在我有兩個表單,一個主表單和另外一個副表單,我已經在主表單中的 var 內設定一個 seat_location: string; 的變數,並在一個procedure中設定 seat_location:='A1'; ,但是副表單卻沒辦法印出來 A1 這個內容,請問該如何設定才對@@? 謝謝~
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-12-04 09:24:10 IP:203.73.xxx.xxx 未訂閱
 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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-12-04 19:48:42 IP:219.86.xxx.xxx 訂閱
感謝回覆,我有試在按鈕裡可以正常作用,但是在 FormCreate的procedure中卻沒辦法顯示出來,請問該如何進一步的修改@@? 謝謝~
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-12-05 08:25:08 IP:61.59.xxx.xxx 未訂閱
請貼上Source讓網友Debug!
===================引 用 liannfs 文 章===================
感謝回覆,我有試在按鈕裡可以正常作用,但是在 FormCreate的procedure中卻沒辦法顯示出來,請問該如何進一步的修改@@? 謝謝~
------
What do we live for if not to make life less difficult for each other?
liannfs
一般會員


發表:3
回覆:6
積分:1
註冊:2007-12-04

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-12-05 18:14:42 IP:219.86.xxx.xxx 訂閱
~這是副表單~
[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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-12-05 20:19:59 IP:61.63.xxx.xxx 訂閱
我猜想,在您專案中  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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-12-05 23:03:11 IP:61.31.xxx.xxx 訂閱
我的意思就是你說的那樣@@!! 但是我看不太懂你的作法@@" 因為我在 order_1這邊的type放了 procedure FormShow(Sender: TObject); 然後之後再加 

procedure Torder.FormShow(Sender: TObject);
begin
Label9.Caption:=Form1.seat_location;
end;

這樣還是沒有出來 0.0" 請問正確的改法應該是如何呢?謝謝~
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-12-06 09:06:16 IP:203.73.xxx.xxx 未訂閱
在 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

發送簡訊給我
#9 引用回覆 回覆 發表時間:2007-12-18 23:06:22 IP:219.87.xxx.xxx 訂閱
 謝謝回覆,但是我還是改不出來@@ 還來就寫在timer裡面,也是可以達到目的,只是方法有點濫.../_\
系統時間:2024-04-18 15:46:19
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!