Instance of Windows::Form

  • Thread starter Thread starter Michael Zeile
  • Start date Start date
M

Michael Zeile

hi there,

i have the following problem:

i have 2 normal Windows Forms classes, declared as:

public __gc class MainForm : public System::Windows::Forms::Form
and
public __gc class FormLayout : public System::Windows::Forms::Form

my application starts like this:

....
MainForm *myform;
myform = new MainForm();
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(myform);
....

somewhere in my MainForm class, i have the following code:
....
FormLayout *formLayout;
formLayout = new FormLayout();
formLayout->Show();
formLayout->InitGL();
....etc...

now my problem: i want to send status messages or change the behavior of
my MainForm window by pressing a key in my FormLayout form, but i can't
because i don't know the instance of my MainForm ... because __gc
objects can't be declared global. so how do i get the instance of my
form generated by Application::Run() ?

any suggestions ?

thx a LOT...

bye
michael
 
try the gcroot<managedClass*> class template. This makes it possible to
declare __gc classes global

/Morten
 
You could also force the constructor to take a System::Object gc* parameter
and call the layout form as such...

FormLayout *formLayout;
formLayout = new FormLayout(this); // where 'this' is the MainForm class
instance


.....then in the FormLayout class, declare a member as "System::Object gc*
parentMember". Then you can save the memory address of the parent class in
your FormLayout constructor....

FormLayout (System::Object __gc *parent)
{
parentMember = parent;
}

....Then whenever you have to call a function in the parent, you can do this
(Make sure you put the #include "MainForm.h" in the FormLayout.cpp file...

MainForm *mf = dynamic_cast<MainForm *>(parentMember);

// check for valid ptr to parent before referencing
if (mf) {

mf->SomeFunction(someParementer);

}

Hope this helps.....

-TGF
 
Back
Top