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

又一個Form與Form之間傳值的問題

尚未結案
JPTseng
一般會員


發表:14
回覆:22
積分:7
註冊:2004-10-27

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-11-01 19:31:30 IP:140.124.xxx.xxx 未訂閱
之前查了以前的文章都著重在dynamic from,而我是使用Button來執行 Form2->ShowModal(); 這方式來完成, 1. 我在各個.cpp在都採用#include "Unit1.h" #include "Unit2.h" 會依然沒有效果嗎 ? (我看之前的文章是說Form1可#include Unit2.h ,但反過來無用) 2. 像我這樣如何共用同一個struct 或者變數? (ex. 在From1裡算加法,再到From2裡算乘法 ) 3. 以下是我看之前文章說的 《 建立全域變數放置你的SubFounction內 SubFunction.h extern AnsiString aaa; extern bool bbb; extern int ccc; SubFunction.cpp #pragma AnsiString aaa; bool bbb; int ccc; 只要你任何.cpp內Include "SubFunction.h" 你就可以引用全域變數 》 請問這要放置在程式哪裡 ?? 謝謝
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-11-01 21:05:39 IP:61.64.xxx.xxx 未訂閱
// Global.h
// 用來宣告全域變數    // 宣告一個全域變數
extern int iCount;    /////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Unit1.cpp    #include 
#pragma hdrstop    #include "Unit1.h"
#include "Unit2.h"    #include "Global.h"    //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;    // 定義只能有一次
// 定義Global.h裡的 iCount = 0;
int iCount = 0;    void __fastcall TForm1::Button1Click(TObject *Sender)
{
  // 打開 Form2. 並等待按下Form2的Button1
  Form2->ShowModal();      // 引用 Global.h裡的iCount
  iCount  ;
  ShowMessage(IntToStr(iCount));  
}    //---------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------------
// Unit2.cpp    #include 
#pragma hdrstop    #include "Unit2.h"    #include "Global.h"    //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------    void __fastcall TForm2::Button1Click(TObject *Sender)
{
  // 引用 Global.h裡的iCount
  iCount  ;
  ShowMessage(IntToStr(iCount));      // 關閉Form2, 將控制權還給Form1
  Close();    
}
//---------------------------------------------------------------------------    
jason8668
一般會員


發表:17
回覆:31
積分:9
註冊:2003-11-01

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-11-03 13:18:32 IP:211.23.xxx.xxx 未訂閱
你可以先把你要宣告成全域變數的變數儲存成一個*.h的檔案,然後只要在你需要用到這個變數的程式內include這個.h檔就可以了~ 宣告變數的方法為 extern 變數名稱;
hdilwy
初階會員


發表:18
回覆:65
積分:41
註冊:2004-08-31

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-11-05 00:52:30 IP:219.68.xxx.xxx 未訂閱
引言: 你可以先把你要宣告成全域變數的變數儲存成一個*.h的檔案,然後只要在你需要用到這個變數的程式內include這個.h檔就可以了~ 宣告變數的方法為 extern 變數名稱;
extern 變數型態?變數名稱; 請問一下可以宣告指標嗎?
jason8668
一般會員


發表:17
回覆:31
積分:9
註冊:2003-11-01

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-11-05 12:41:09 IP:211.23.xxx.xxx 未訂閱
引言: extern 變數型態?變數名稱; 請問一下可以宣告指標嗎?
當然可以ㄚ~
JPTseng
一般會員


發表:14
回覆:22
積分:7
註冊:2004-10-27

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-11-07 13:55:37 IP:140.124.xxx.xxx 未訂閱
我在Form1上是設定一個矩陣來給定值,這矩陣我是定為extern 但是到From2上時要取出來卻都是 0 why ?
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-11-07 14:10:59 IP:61.64.xxx.xxx 未訂閱
引言: 我在Form1上是設定一個矩陣來給定值,這矩陣我是定為extern 但是到From2上時要取出來卻都是 0 why ?
您是怎麼宣告的呢? 貼出來才好幫你
JPTseng
一般會員


發表:14
回覆:22
積分:7
註冊:2004-10-27

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-11-07 14:28:19 IP:140.124.xxx.xxx 未訂閱
在Fil1.h : struct ss { int a; int b; };    struct ss ff;    在From1 : //---------------------------------------------------------------------------    #include  #pragma hdrstop #include "Unit1.h" #include "File1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; extern struct ss ff; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {} //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { ff.a=StrToInt(Edit1->Text); ff.b=StrToInt(Edit2->Text); Form2->Show(); } //------------------------------------------------------------------- 在Form2 : //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" #include "File1.h" #include "Unit2.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm2 *Form2; extern struct ss ff; //------------------------------------------------------------------- __fastcall TForm2::TForm2(TComponent* Owner) : TForm(Owner) {} //------------------------------------------------------------------- void __fastcall TForm2::Button1Click(TObject *Sender) { Edit1->Text=IntToStr(ff.a); Edit2->Text=IntToStr(ff.b); } //------------------------------------------------------------------- 發表人 - JPTseng 於 2004/11/07 14:54:55
Zard
尊榮會員


發表:24
回覆:396
積分:539
註冊:2003-11-26

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-11-07 16:37:26 IP:61.64.xxx.xxx 未訂閱
紅色部份是幫你改過的.     
在Fil1.h :
struct ss
{
  int a;
  int b;
};    extern struct ss ff;    在From1 :
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
#include "File1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;    //extern struct ss ff;
struct ss ff
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{}
//---------------------------------------------------------------------------    void __fastcall TForm1::Button1Click(TObject *Sender)
{
  ff.a=StrToInt(Edit1->Text);
  ff.b=StrToInt(Edit2->Text);      Form2->Show();
}
//-------------------------------------------------------------------    在Form2 :
//---------------------------------------------------------------------------    #include 
#pragma hdrstop    #include "Unit1.h"
#include "File1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//extern struct ss ff;
//-------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{}
//-------------------------------------------------------------------    void __fastcall TForm2::Button1Click(TObject *Sender)
{
  Edit1->Text=IntToStr(ff.a);
  Edit2->Text=IntToStr(ff.b);
}
//-------------------------------------------------------------------
JPTseng
一般會員


發表:14
回覆:22
積分:7
註冊:2004-10-27

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-11-07 16:42:35 IP:140.124.xxx.xxx 未訂閱
Zard哥~~~~ 謝謝您~~~^^
系統時間:2024-04-20 5:06:42
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!