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

關於兩個Form同時存取全域變數的問題??

尚未結案
john586
一般會員


發表:6
回覆:5
積分:2
註冊:2003-03-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-10-16 18:05:21 IP:218.172.xxx.xxx 未訂閱
現在假設Project1裡有兩個Form,小弟要如何宣告一個全域變數並且可讓兩個Form同時存取此一變數呢?? 發表人 - taishyang 於 2003/10/16 18:32:05
gemi0305
版主


發表:81
回覆:564
積分:629
註冊:2003-05-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-10-16 19:27:38 IP:61.224.xxx.xxx 未訂閱
你可以另外寫一個h檔,把你要的全域變數放在裡面, 就可以讓這兩個form來使用。 如: File1.h 
#ifndef File1H
#define File1H    int sInt;    #endif     
Unit1.cpp
 
#include 
#pragma hdrstop    #include "Unit1.h"
#include "Unit2.h"
#include "File1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    sInt = 1;
}
//---------------------------------------------------------------------------    void __fastcall TForm1::Button1Click(TObject *Sender)
{
     Form2->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
     ShowMessage(IntToStr(sInt));
     sInt = 3;
}
Unit2.cpp
 
#include 
#pragma hdrstop    #include "Unit2.h"
#include "Unit1.h"
extern sInt;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------    void __fastcall TForm2::Button1Click(TObject *Sender)
{
     ShowMessage(IntToStr(sInt));
     sInt = 2;    }
還有另一個方法,兩個form直接互相存取彼此的變數、結構和vcl~ 參考以下文章:
john586
一般會員


發表:6
回覆:5
積分:2
註冊:2003-03-24

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