[問題] 請教BCB中不同Form中變數的傳遞
想請問兩個不同的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
07/23 22:41, 1F
推
07/23 23:50, , 2F
07/23 23:50, 2F
→
07/24 08:56, , 3F
07/24 08:56, 3F
→
07/24 08:56, , 4F
07/24 08:56, 4F
→
07/24 08:58, , 5F
07/24 08:58, 5F
→
07/24 08:59, , 6F
07/24 08:59, 6F
推
07/24 09:30, , 7F
07/24 09:30, 7F
→
07/24 09:30, , 8F
07/24 09:30, 8F
推
08/17 00:34, , 9F
08/17 00:34, 9F
→
08/17 00:34, , 10F
08/17 00:34, 10F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章