Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Relating VC.net forms

hi Jonnin, Ivan, Danny, Code nerd and everyone.
The problem is related VC.net.
I have few forms which I want to call each other
for example from 1 calls form2 and form2 calls form3
but form3 has a back button so it calls form2 too and
it needs form2 object to get some other info of form2 as well.
As long as I do #include for in one direction it works,
but if I try including e.g
form2 include form3 but when i try to include form3 to
form2 it gives error. In Vb.net its too simple to call
forms, but I am stucked with VC.net.
Is there some inheritence or dependancy problem,
any solution any suggestion please.
Cheers to all,
nadz
[688 byte] By [nadz] at [2007-11-11 8:48:44]
# 1 Re: Relating VC.net forms
You haven't specified the error code so I can't be sure what the problem is.
But you could try supplying pointers as arguments instead of simple values.
For instance, from Form2 you can call Form3 method and pass the address of a info structure thus eliminating the need for Form3 to know about Form2 or Form1 for that matter.
In the example below Form2 knows about Form3 but Form3 doesn't know about Form2

typedef struct {
some struct declarations here
} Info, *PInfo;
class TForm2
{
Info SomeInfo;
void CallForm3();
};

void TForm2::CallForm3()
{
//it is assumed that Form3 has been created
Form3->Show();
//Init is a member method of Form3
Form3->Init(&SomeInfo); //Form3 can now examine the info and change it if it should
}

I' m not sure if this is what you're trying to do

Regards Ivan
Ivan** at 2007-11-11 21:00:56 >
# 2 Re: Relating VC.net forms
I'm not sure I understand what the problem is, but perhaps you should look into forward declarations. This is the solution for mutual dependencies of declarations, e.g., class A uses B and B uses A.
Danny at 2007-11-11 21:02:02 >
# 3 Re: Relating VC.net forms
Yea thats y i think, then the only solution may be is to build a parent form
which controls all the communication between the froms. But that will make application slow and more verbose.
Please any suggestion, I can call FormB from FormA but y on earth cannot call FormA from FormB.
Thanks all.
nadz at 2007-11-11 21:03:06 >
# 4 Re: Relating VC.net forms
You can also write a separate cpp (which doesn't contain a form) file and include this file in every cpp file (formsx.cpp) and then simply call some method in the separate cpp which transfers control to some other form's cpp.
form2->separeate.cpp-somefunction->form1
I've used this approach so I know it works
Ivan** at 2007-11-11 21:04:01 >
# 5 Re: Relating VC.net forms
I am just stucked Ivan.
Now I had made a single cpp file with all the functions in it.
Can you tell me please exactly how can I now call all forms
forward and reciprocally.
Can you also tell me one thing that
only my first form.cpp file is has meaningful code as it is the starting form.
can I do coding in other forms.cpp files, do I have to declare extra statements in it?
Thanks for help
Cheers
nadz at 2007-11-11 21:05:00 >
# 6 Re: Relating VC.net forms
Hi nadz
I didn't use that separate file exactly the way you intended. But you may try this:

Form1.cpp and Form1.h
Form2.cpp and Form2.h
separate.cpp and separate.h

separate.cpp declarations
#include "Form1.h"
#include "Form2.h"

void CallForm1()
{
Form1->Show();
}
void CallForm2()
{
Form2->Show();
}

separate.h declarations
#ifndef separateH
#define separateH
void CallForm1();
void CallForm2();
#endif

Now both Form2.cpp and Form1.cpp must include separate.h.

Now from Form2.cpp you would simply call CallForm1 and from Form1.cpp call CallForm2
For instance
void TForm2::Caller()
{
CallForm1();
}
void TForm1::Caller()
{
CallForm2();
}
Ivan** at 2007-11-11 21:06:00 >
# 7 Re: Relating VC.net forms
Thanks Ivan,
I will try to understand and implement this thing.
Thanks for explaining it to me.
nadz at 2007-11-11 21:07:04 >
# 8 Re: Relating VC.net forms
hi Ivan,
I have a problem,
I have a .cpp file. In this file I want to do like this
form2* f2= f2->textbox->get_text();
to get the text, I have included the form2.h in the file.
but I get error that variable form2 is undefined.
If I try to do it reverse and call some settext function of .cpp file
from form2 it works but the value I get is local to the function and I cannot use it outside of funtion this value. Even if I assign the value to a global variable outside the function it is initialized again.
Please suggest a solution.
Cheers
nadz
nadz at 2007-11-11 21:08:02 >
# 9 Re: Relating VC.net forms
form2.h must contain the following statement just after TForm2 class declarations like:
class TForm2: public TForm
{
//class declarations here
};

extern TForm2* form2; //this is required

separate.cpp
#include "form2.h"
//TForm2* form2; //this should not be required
void SomeFunc()
{
form2->TextBox1 = "text"; //form2 must be accessible here
}

I tested this 2 days ago and it worked as expected. Of course I'm using a different compiler but I don't think it matters. So check for the extern keyword and if that fails send a complete listing to me and I'll have a look at it
Ivan** at 2007-11-11 21:09:10 >
# 10 Re: Relating VC.net forms
hi Ivan,
it seems the extern is not allowed in vc.net as compiler gives an error.
: cannot declare a global or static managed type object or a __gc pointer:
do not know what to do. y mircosoft made such a simple task so buggy.
any solution.
thanking you.
nadz
nadz at 2007-11-11 21:10:06 >