Board index » cppbuilder » SubForm

SubForm


2006-11-17 12:37:08 PM
cppbuilder60
hi,
I always want to know , if show a subform(Form2) and input some data, then
how can I
acquire these data in Form1??
thanks.
 
 

Re:SubForm

"jeanet" < XXXX@XXXXX.COM >wrote in message
Quote
I always want to know , if show a subform(Form2) and input
some data, then how can I acquire these data in Form1??
Simply access the SubForm's controls like any other class members. They are
publically accessible. For example:
procedure TForm1.Button1Click(Sender: TObject);
var
S: String;
SubForm: TSubForm;
begin
SubForm := TSubForm.Create(nil);
try
if SubForm.ShowModal = mrOk then
begin
S := SubForm.Edit1.Text;
// use S as needed ...
end;
finally
SubForm.Free;
end;
end;
This is basic OOP programming.
Gambit
 

Re:SubForm

"jeanet" < XXXX@XXXXX.COM >wrote in message
Quote
I always want to know , if show a subform(Form2) and input
some data, then how can I acquire these data in Form1??
You can access the SubForm's controls like any other class members. They
are publically accessible. For example:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TSubForm *SubForm = new TSubForm(NULL);
try
{
if( SubForm->ShowModal() == mrOk )
{
AnsiString S = SubForm->Edit1->Text;
// use S as needed ...
}
}
__finally
{
delete SubForm;
}
}
This is basic OOP programming.
Gambit
 

{smallsort}

Re:SubForm

oh,I see, you are too nice.
thanks!
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >写入消息新闻: XXXX@XXXXX.COM ...
Quote

"jeanet" < XXXX@XXXXX.COM >wrote in message
news:455d3c51$ XXXX@XXXXX.COM ...

>I always want to know , if show a subform(Form2) and input
>some data, then how can I acquire these data in Form1??

You can access the SubForm's controls like any other class members. They
are publically accessible. For example:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TSubForm *SubForm = new TSubForm(NULL);
try
{
if( SubForm->ShowModal() == mrOk )
{
AnsiString S = SubForm->Edit1->Text;
// use S as needed ...
}
}
__finally
{
delete SubForm;
}
}

This is basic OOP programming.


Gambit



 

Re:SubForm

As an alternative use extern. Maybe not good OOP practice, but it works.
Description
Use the extern modifier to indicate that the actual storage and initial value of a variable, or body of a function, is defined in a separate source code module. Functions declared with extern are visible throughout all source files in a program, unless you redefine the function as static.
The keyword extern is optional for a function prototype.
george
Quote
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >写入消息新闻: XXXX@XXXXX.COM ...
>You can access the SubForm's controls like any other class members. They
>are publically accessible. For example:
>
>This is basic OOP programming.
>
>
>Gambit
>
>
>


 

Re:SubForm

Where do I declare the extern variable if I use it?
CPP file of Head file? Which part?
thanks!
"george" < XXXX@XXXXX.COM >写入消息新闻:455dc9a4$ XXXX@XXXXX.COM ...
Quote

As an alternative use extern. Maybe not good OOP practice, but it works.

Description

Use the extern modifier to indicate that the actual storage and initial
value of a variable, or body of a function, is defined in a separate
source code module. Functions declared with extern are visible throughout
all source files in a program, unless you redefine the function as static.

The keyword extern is optional for a function prototype.


george

>"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >写入消息新闻: XXXX@XXXXX.COM ...
>>You can access the SubForm's controls like any other class members.
>>They
>>are publically accessible. For example:
>>
>>This is basic OOP programming.
>>
>>
>>Gambit
>>
>>
>>
>
>

 

Re:SubForm

"jeanet" < XXXX@XXXXX.COM >wrote:
Quote

Where do I declare the extern variable if I use it?
First, define it in one unit (cpp) and one unit only.
Then, in the additional units where you want access to it,
you add the key word 'extern'. For example:
// in the main form's unit
int X = 0;
// in Form2's unit;
extern int X;
~ JD
Please see my reply to you in your Form->Visible thread.
 

Re:SubForm

Yes, of course that works fine.
When I have several units all using a batch of common variables
I define them in unit1 (for example) and then write a special
header which lists all the extern variables. Then I include
the special header in each of the other units that can all
share the variables defined in unit1.
Unit1.cpp
int aa,an,bak,bfam;
Special.h
extern int aa,an,bak,bfam;
Unit2.cpp
#include "special.h"
I repeat this for units 2,3,4 etc. without having to list all
the variables in each unit.
I hope you understand this.
George
"JD" < XXXX@XXXXX.COM >wrote:
Quote


First, define it in one unit (cpp) and one unit only.

Then, in the additional units where you want access to it,
you add the key word 'extern'. For example:

// in the main form's unit
int X = 0;

// in Form2's unit;
extern int X;

~ JD


 

Re:SubForm

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote

[...] And don't forget to include that header too in unit1.
Are you sure about that?
~ JD
 

Re:SubForm

george wrote:
Quote
I define them in unit1 (for example) and then write a special
header which lists all the extern variables. Then I include
the special header in each of the other units that can all
share the variables defined in unit1.
Exactly. That's the way to go. And don't forget to include
that header too in unit1.
Hans.
 

Re:SubForm

JD wrote:
Quote
Hans Galema < XXXX@XXXXX.COM >wrote:
>[...] And don't forget to include that header too in unit1.

Are you sure about that?
Yes.
Indeed. Just technically it is not needed mostly (exeptions see
my post in the students group today).
But it prevents errors. Especially if one later changes
someting in the .cpp file. With an included .h file the
compiler will warn directly if the .h is not consistent
with the .cpp.
Hans.
 

Re:SubForm

I tend to agree with JD. If you include the header with the
extern in unit1, you effectively have the same variable
defined twice in the unit, with and without extern.
I have tested it just now and my compiler gives me multiple
declaration errors.
George
Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
JD wrote:
>Hans Galema < XXXX@XXXXX.COM >wrote:
>>[...] And don't forget to include that header too in unit1.
>
>Are you sure about that?

Yes.

Indeed. Just technically it is not needed mostly (exeptions see
my post in the students group today).

But it prevents errors. Especially if one later changes
someting in the .cpp file. With an included .h file the
compiler will warn directly if the .h is not consistent
with the .cpp.

Hans.
 

Re:SubForm

I certanly didn't do that.
What else was JD referring to?
George
Hans Galema < XXXX@XXXXX.COM >wrote:
Probably you did put
Quote
the real declarations also in the header.

Moreover I think that JD referred to something else.

Hans.
 

Re:SubForm

I do not have a problem. One of the things I use extern with is a structure; maybe that is what causes your idea to fail.
George.
Hans Galema < XXXX@XXXXX.COM >wrote:
Quote

But you did something not advised by me what causes your problem.


Actualy you could not agree with him as he did not state anything.
He just asked. The rest is your imagination.

Hans.
 

Re:SubForm

george wrote:
Quote
I tend to agree with JD. If you include the header with the
extern in unit1, you effectively have the same variable
defined twice in the unit, with and without extern.
I have tested it just now and my compiler gives me multiple
declaration errors.
That is impossible if the headeer contains only extern declarations.
You should look for things you did wrong. Probably you did put
the real declarations also in the header.
Moreover I think that JD referred to something else.
Hans.