0

表格 1: (frmStart)

void __fastcall TfrmStart::btnRunClick(TObject *Sender)
{
    frmStart->Hide();
    Application->CreateForm(__classid(TfrmRunning), &frmRunning);
}

表格 2:(跑步)

void __fastcall TfrmRunning::FormCreate(TObject *Sender)
{
      frmRunning->Show(); 
 //Here i do a lot of stuff to run my main program
}

问题是我的第一个表单正确加载和隐藏。当第二个表单加载时,不会显示任何组件,只显示一个看起来像是崩溃的 GUI/。当程序真正结束时,GUI 会返回到正常状态。

我哪里做错了?

4

2 回答 2

2

在这种情况OnCreate下,frmRunning变量尚未分配,这就是代码崩溃的原因。由于您已经在类中,只需使用方法的this指针:

void __fastcall TfrmRunning::FormCreate(TObject *Sender) 
{ 
      //frmRunning->Show();  
      this->Show();  
} 

或者简单地说:

void __fastcall TfrmRunning::FormCreate(TObject *Sender) 
{ 
      //frmRunning->Show();  
      Show();  
} 

在方法中做同样的事情btnRunClick()- 使用this指针而不是frmStart变量:

void __fastcall TfrmStart::btnRunClick(TObject *Sender) 
{ 
    //frmStart->Hide(); 
    this->Hide(); 
    ...
} 

或者:

void __fastcall TfrmStart::btnRunClick(TObject *Sender) 
{ 
    //frmStart->Hide(); 
    Hide(); 
    ...
} 

最后,您应该使用new运算符而不是方法,并且在表单完成初始化之前TApplication::CreateForm()不要调用方法:Show()

void __fastcall TfrmStart::btnRunClick(TObject *Sender)   
{   
    frmRunning = new TfrmRunning(Application);
    frmRunning->Show();   
    Hide();   
}   

__fastcall TfrmRunning::TfrmRunning(TComponent *Owner)   
    : TForm(Owner)
{   
   // initialize this Form as needed...
}   
于 2012-07-17T19:54:44.953 回答
1

Using FormCreate isn't the right place to set up your form. Use the c++ style constructor instead. I'd also probably call the Show method from somewhere else, such as another form's button handler, or the form's constructor. I would then use the OnShow Event to do the "lot of stuff"

于 2012-07-17T15:19:22.843 回答