| View previous topic :: View next topic |
| Author |
Message |
Jack Mills Guest
|
Posted: Tue Jun 22, 2004 12:24 pm Post subject: Find an Instance of a form |
|
|
How do I find out if an instance of a form exists? & if it does show it
rather than create another new one!
I presume it involves "this" & "Owner", but my trial & error attempts are
not working out.
I have this on a form, this form is not a MDIForm.
The TEM_DetailForm style, is set to MDIChild.
void __fastcall TEditorChildDG::Edit1Click(TObject *Sender, int LVIndex)
{
TEM_DetailForm *EM_Detail;
EM_Detail = new TEM_DetailForm(this);
EM_Detail->Query1->DatabaseName = Query1->DatabaseName;
EM_Detail->Query1->Tag = LVIndex; ;
EM_Detail->Caption = Caption;
EM_Detail->Show();
}
I want to insert before that code, in English
If TEM_DetailForm that I created exists & its Query1->Tag = LVIndex Show it
else
create it as above.
Thanks for any help
Jack
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Jun 22, 2004 1:00 pm Post subject: Re: Find an Instance of a form |
|
|
Jack Mills wrote:
| Quote: | How do I find out if an instance of a form exists?
|
The simplest method is to have the form's constructor set a global flag
to indicate that it exists. In point of fact the IDE gives you such a
flag. Each form has an instance variable at the top. In this case it
should be declared as:
TEM_DetailForm * EM_DetailForm;
so:
void __fastcall TEditorChildDG::Edit1Click(TObject *Sender, int LVIndex)
{
if( !EM_DetailForm )
EM_DetailForm = new TEM_DetailForm(this);
EM_DetailForm->Query1->DatabaseName = Query1->DatabaseName;
// etc...
}
Then in the destructor or OnFormDestroy handler add:
EM_Detail=NULL;
Checking the tag is a bit more complicated if you can have multiple
detail forms visable with different tags. For that you'd need to
declare your own global array of form pointers or std::vector. Then you
iterate through that list checking for a details form with the tag set.
Each form constructor would add its 'this' pointer to the array and the
destructor would remove it.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Jun 22, 2004 1:03 pm Post subject: Re: Find an Instance of a form |
|
|
Andrue Cope [TeamB] wrote:
| Quote: | Then in the destructor or OnFormDestroy handler add:
EM_Detail=NULL;
|
Oops I forgot to add that you should ensure the variable is set to NULL
at program start as well.
Change:
TEM_DetailForm * EM_DetailForm;
to
TEM_DetailForm * EM_DetailForm=NULL;
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Jack Mills Guest
|
Posted: Tue Jun 22, 2004 1:27 pm Post subject: Re: Find an Instance of a form |
|
|
Thanks Andrue
| Quote: | Checking the tag is a bit more complicated if you can have multiple
detail forms visible with different tags. For that you'd need to
declare your own global array of form pointers or std::vector.
|
Unfortunately I can have multiple forms visible with different tags.
I will read up on std::vector
I can make each forms Caption unique by adding the LVIndex integer, does
that make it simpler to find the form?
Jack
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Jun 22, 2004 1:42 pm Post subject: Re: Find an Instance of a form |
|
|
Jack Mills wrote:
| Quote: | I can make each forms Caption unique by adding the LVIndex integer,
does that make it simpler to find the form?
|
It could, yes but another solution is actually to iterate through the
components owned by the application. You could probably use the
following (untested code):
for( int index=0;
index<Application->ComponentsCount;
++index )
{
TEM_DetailForm * tmp=
dynamic_cast<TEM_DetailForm *>(
Application->Components[ index ] );
if( tmp )
{
// tmp now points to an instance of a TEM_DetailForm.
}
}
If your app is an MDI app and these are MDI children you should iterate
through your main form's MDIChildren array in much the same way.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Jack Mills Guest
|
Posted: Tue Jun 22, 2004 2:29 pm Post subject: Re: Find an Instance of a form |
|
|
Thanks Andrue
Should your code example be placed here <<<<<< ?
void __fastcall TEditorChildDG::Edit1Click(TObject *Sender, int LVIndex)
{
<<<<<<<
TEM_DetailForm *EM_Detail;
EM_Detail = new TEM_DetailForm(this);
EM_Detail->Query1->DatabaseName = Query1->DatabaseName;
EM_Detail->Query1->Tag = LVIndex; ;
EM_Detail->Caption = Caption;
EM_Detail->Show();
}
When placed here <<< the ComponentCount is always 3, no matter how many
forms are open.
| Quote: | If your app is an MDI app and these are MDI children you should iterate
through your main form's MDIChildren array in much the same way.
|
The app is a MDI app but the form I am checking for the EM_Detail form is
not a MDIForm (parent) it is also a MDIChild.
Jack
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Tue Jun 22, 2004 2:54 pm Post subject: Re: Find an Instance of a form |
|
|
Jack Mills wrote:
| Quote: | Should your code example be placed here <<<<<< ?
|
Yes.
| Quote: | When placed here <<< the ComponentCount is always 3, no matter how many
forms are open.
|
Do use Screen->Forms[] instead of Application->Components[].
| Quote: | The app is a MDI app but the form I am checking for the EM_Detail form is
not a MDIForm (parent) it is also a MDIChild.
|
Screen->Forms[] does not care about being MDI or not.
Hans.
|
|
| Back to top |
|
 |
Jack Mills Guest
|
Posted: Tue Jun 22, 2004 3:43 pm Post subject: Re: Find an Instance of a form |
|
|
Thanks Hans
I did this, which appears to work well.
for(int index=0; index < Application->ComponentCount; ++index )
{
TEM_DetailForm *tmp = dynamic_cast<TEM_DetailForm
*>(Screen->Forms[index]); //Application->Components
if( tmp )
{
// tmp now points to an instance of a TEM_DetailForm.
if(tmp->Query1->Tag == LVIndex)
{
tmp->BringToFront();
return;
}
}
}
But is the Application->ComponentCount correct in the for loop? I tried
Screen->Forms->MDIChildCount but the complier complained of "Too few
parameters".
Otherwise it seems to function as desired.
Jack
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Jun 22, 2004 3:43 pm Post subject: Re: Find an Instance of a form |
|
|
Hans Galema wrote:
| Quote: | When placed here <<< the ComponentCount is always 3, no matter how
many forms are open.
Do use Screen->Forms[] instead of Application->Components[].
|
/That's/ where it is. I looked and looked for that and couldn't find it.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Jun 22, 2004 3:45 pm Post subject: Re: Find an Instance of a form |
|
|
Jack Mills wrote:
| Quote: | But is the Application->ComponentCount correct in the for loop?
|
I wouldn't think so.
Application::ComponentCount is the total in Application::Components.
A little deductive reasoning suggests that TScreen::Forms would have
its total specified by...
...TScreen::FormCount
:)
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
Jack Mills Guest
|
Posted: Tue Jun 22, 2004 4:08 pm Post subject: Re: Find an Instance of a form |
|
|
Of Course, it takes a little time for me to get there.
for(int index=0; index < Screen->FormCount; ++index )
{
TEM_DetailForm *tmp = dynamic_cast<TEM_DetailForm
*>(Screen->Forms[index]);
if( tmp )
{
// tmp now points to an instance of a TEM_DetailForm.
if(tmp->Query1->Tag == LVIndex)
{
tmp->BringToFront();
return;
}
}
}
Many Thanks to you Andrue & to you Hans.
Jack
|
|
| Back to top |
|
 |
Andrue Cope [TeamB] Guest
|
Posted: Tue Jun 22, 2004 4:10 pm Post subject: Re: Find an Instance of a form |
|
|
Jack Mills wrote:
| Quote: | Of Course, it takes a little time for me to get there.
|
No problem. I was getting a little frustrated at not finding the
property Hans did. I /knew/ I'd used one once but it must have been too
long ago.
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
|
|
| Back to top |
|
 |
|