各位大神好。遇到一個問題,糾結了好幾年了。
問題描述:
表單1,name=Form1,表單2,name=Form2。如果表單1,2都是靜態創建的,那么假如從表單1去訪問表單2中的一個按鈕Button1,那么我直接可以在表單1中的函式中寫Form2->Button1=“你好”.
現在的問題是如果表單1是動態創建的,假如是在表單0中創建的,創建代碼:
*TForm *pForm;
pForm=new TForm1(Application);
pForm->ShowModal();
表單2是有表單1中的一個函式動態創建的。創建代碼相同。如果我要在表單2關閉時處理一些可能會影響表單1中的資料時,需要訪問表單1中的控制元件。就是這個問題,我試了好幾種方法都不行。不知哪位大神可以幫助解答,萬分感謝。
編程環境:CB6.
uj5u.com熱心網友回復:
*TForm *pForm;pForm=new TForm1(Application);
pForm->ShowModal();
注意一下,pForm是區域變數哦,你需要創建全域的變數來保存form1 和form2
這樣就沒問題拉
uj5u.com熱心網友回復:
我先試試,多謝
uj5u.com熱心網友回復:
直接訪問表單1上面的資源包含表單1的cpp檔案也可以,不過會死的很慘,像這種表單之間的訪問建議你用傳引數的辦法void __fastcall TKeyBao::ScannerButtonClick(TObject *Sender)在表單的建構式中也做相應處理
{
ScannerBarCode(ScanTimeOut*100);
}
//---------------------------------------------------------------------------
bool __fastcall TKeyBao::ScannerBarCode(int TimeOut)
{
bool Ret = false;
MsgEdit->Text = "";
TCaptureForm *Capture = new TCaptureForm(this,TimeOut);
if(Capture->ShowModal() == mrOk)
{
MsgEdit->Text = Capture->EnCodeStr.Trim() ;
Ret = true;
}
delete Capture;
return Ret;
}
__fastcall TCaptureForm::TCaptureForm(TComponent* Owner,unsigned int TimeOut)對于表單關閉后變數等回傳可以直接用公共變數,在頭檔案中這么宣告
: TForm(Owner)
{
m_capture = NULL;
EnCodeStr = "";
TimeOutCount = (TimeOut*1000)/CaptureTimer->Interval;
}
public: // User declarations
String EnCodeStr;
__fastcall TCaptureForm(TComponent* Owner,unsigned int TimeOut);
uj5u.com熱心網友回復:
動態創建的最好用創建的視窗修改主視窗的資料。如果非你那樣用不可,就在主視窗里面宣告一個form2的指標,主視窗去呼叫。
TForm2 *pForm2;
初始化:
form1->oncreate里面pForm2 = NULL
創建:
if(pForm2 == NULL) pForm2 = new TForm2(...);
呼叫:
if(pForm2)pForm2->Button1->Click();
銷毀:
if(pForm2)delete pForm2; pForm2 = NULL;
這樣做一個指標只能指定一個視窗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/107087.html
標籤:基礎類
上一篇:三道面試題,求助大神解答
