[問題] 請教BCB中不同Form中變數的傳遞

看板C_and_CPP (C/C++)作者 (5588clcj)時間16年前 (2009/07/23 21:09), 編輯推噓3(307)
留言10則, 5人參與, 最新討論串1/1
想請問兩個不同的Form,元件數值傳遞的問題 一個簡單的程式範例如下: 假設我原先有一個Form1,我想由Form1叫出另一個Form2 就在Form1設計完後New一個新的Form2 並在Project的Options裡Form的設定把Form1設定為Auto-create forms Form2設定為Available forms 接著在Unit1.cpp裡 #include "Unit2.h" 並設定一個按鈕啟動Form2,程式碼如下 void __fastcall TForm1::Button3Click(TObject *Sender) { TForm2 *Form2; Form2 = new TForm2(Application); Form2->Show(); } 最後於Form2的Onclose事件中編輯程式碼如下 void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action) { Action = caFree; } 即完成由Form1以一個Button呼叫Form2的簡單程式 ------------------------------------------------------------------------------ 但我想請教的是,假設我今天在Form1中放一個Edit用來輸入我的要的數值 而在Form2中將Form1的Edit做加減乘除的動作並於Form2中一個Label顯示出答案 這樣應該怎麼做 我是有試過這樣的修改 在Unit1.cpp中: //$$---- Form CPP ---- //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "Unit2.h" double x=30; double y=x/2; 在Unit2.cpp中: void __fastcall TForm2::Button1Click(TObject *Sender) { extern double x; extern double y; Label3->Caption = y; } 是成功的....... 但.... 如果在Unit1.cpp中 將double x=30; double y=x/2; 改成 double x = StrToFloat(Edit1->Text); double y=x/2; 就會出錯... 有沒有人能幫我解答? 不好意思因為急所以文章寫得很冗長又有點亂,請見諒... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.127.33.83

07/23 22:41, , 1F
可以直接extern TForm1 *Form1 就都可以用了吧
07/23 22:41, 1F

07/23 23:50, , 2F
在Unit2.cpp中include Unit1.h 就可以使用Form1的東西
07/23 23:50, 2F

07/24 08:56, , 3F
不好意思,兩個方法我都有用了,但還是有錯...
07/24 08:56, 3F

07/24 08:56, , 4F
問題好像出在double x = StrToFloat(Edit1->Text);
07/24 08:56, 4F

07/24 08:58, , 5F
會出現這個ERROR如下:
07/24 08:58, 5F

07/24 08:59, , 6F
[C++ Error]Unit1.cpp(10):E2451 Undefined symbol'Edit'
07/24 08:59, 6F

07/24 09:30, , 7F
Edit1是Form1的沒理由在自己的cpp中找不到Simbol吧@_@"
07/24 09:30, 7F

07/24 09:30, , 8F
看error msg, 您是不是Edit少打了個1啦....@_@"
07/24 09:30, 8F

08/17 00:34, , 9F
應該是他不知道你的Edit1是哪個Form的 要用Form1->Edit1
08/17 00:34, 9F

08/17 00:34, , 10F
我居然回了一年前的文章 ORZ
08/17 00:34, 10F
文章代碼(AID): #1AQ63yht (C_and_CPP)
文章代碼(AID): #1AQ63yht (C_and_CPP)