Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forms

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
My code below has two Forms. On Form1 is a checkbox and in FormClose the checkbox status is saved to a file. At FormCreate the checkbox status is read and if true I wanted to display Form2. When the checkbox is true I get an Access Violation Error. What am I doing wrong?

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
TIniFile *WinIni = new TIniFile("IniTester.ini");

if (CheckBox1->Checked==true)
WinIni->WriteBool ("CheckBox1", "checked", true);
else
WinIni->WriteBool ("CheckBox1", "checked", false);

delete WinIni;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TIniFile *WinIni = new TIniFile("IniTester.ini");

CheckBox1->Checked=(WinIni->ReadBool("CheckBox1","Checked",CheckBox1->Checked));

delete WinIni;
Form1->Refresh();

if (CheckBox1->Checked==true)
{
Form2->Show();
}


}
 
I think the Problem is Form2 is not yet created when you call Form2->Show.

It would be better to put this code into the Form1Show function.


hnd
hasso55@yahoo.com

 
ya check in your project.cpp unit, your form1 is create before other form. You can change the create order of those form.. but the main windows(form) is always the first to be created.. So as Hnd says, u cant call form2 Show method because form2 dont already exist in the form1 create. However u can do your test in the project.cpp instead of in the show event of form1.. espacialy if u plan to show form1 somewhere else.. else u better to do as Hnd says..


//----------------------------------------------------------
#include <vcl\vcl.h>

#include &quot;Unit1.h&quot; //include your units
#include &quot;Unit2.h&quot; //to be able to use form1,form2

#pragma hdrstop
//----------------------------------------------------------
USEFORM(&quot;Unit1.cpp&quot;, Form1);
USERES(&quot;Project1.res&quot;);
USEFORM(&quot;Unit2.cpp&quot;, Form2);
//----------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
//first form to load = main windows.. first window
//that will be show.
Application->CreateForm(__classid(TForm1), &Form1);

Application->CreateForm(__classid(TForm2), &Form2);

//here all windows as been create.. none r shown
//place your code here, for ex:
if (Form1->CheckBox1->Checked)
{
Form2->Show();
//other code .. ex) hide form1,bring form2
//to top..
}
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
//----------------------------------------------------------



jb
 
Thanks, Putting the code in the Form1Show function did the trick.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top