Handle to an opened form

G

Guest

Hi,

I have an C++/CLI MDI application with two Forms A and B.

When I work with Form A I would like to know the handle of Form B (if it is
opened), to use its handle in a delegate that execute a function in Form B.

In example:
DelegateAbc^ d = gcnew DelegateAbc(HANDLE,&FormB::TestB);

Where HANDLE belongs to FormB opened in other moment directly by the main
windows.

Thanks in advance.
 
J

Jeffrey Tan[MSFT]

Hi Tomas,

Based on my understanding, your FormA and FormB are both the child forms of
the main MDI window. Now, you want to obtain the C++/CLI reference to the
FormB if opened. If I have misunderstood you, please feel free to tell me,
thanks.

Normally, when we are creating the FormB instance in the code, we'd better
store FormB's reference in a private field or property in FormA class, so
that the FormA can retrieve FormB's reference in later time.

If you did not store the FormB's reference during creation, you have to
enumerate through the child form collection and get the "FormB" reference
like below:

System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form^ mdiparent=this->MdiParent;

FormB^ formB;
for(int i=0;i<mdiparent->MdiChildren->Length;i++)
{
if(mdiparent->MdiChildren->GetType()->FullName->Contains("FormB"))
{
formB=dynamic_cast<FormB^>(mdiparent->MdiChildren);
}
}
if(formB!=nullptr)
{
MessageBox::Show(formB->Handle::get().ToString());
}
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

Ben Voigt

If you did not store the FormB's reference during creation, you have to
enumerate through the child form collection and get the "FormB" reference
like below:

System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Form^ mdiparent=this->MdiParent;

FormB^ formB;
for(int i=0;i<mdiparent->MdiChildren->Length;i++)
{
if(mdiparent->MdiChildren->GetType()->FullName->Contains("FormB"))


No! Please no! Just leave this out, the dynamic_cast will correctly
determine whether the Form is a FormB. Or use if (...->GetType() ==
FormB::typeid) if you want to avoid matching derived classes. But do not
call string operations for this!
{
formB=dynamic_cast<FormB^>(mdiparent->MdiChildren);
}
}
if(formB!=nullptr)
{
MessageBox::Show(formB->Handle::get().ToString());
}
}

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
G

Guest

Based on my understanding, your FormA and FormB are both the child forms of
the main MDI window. Now, you want to obtain the C++/CLI reference to the
FormB if opened. If I have misunderstood you, please feel free to tell me,
thanks.

Yes this is the situation.
Normally, when we are creating the FormB instance in the code, we'd better
store FormB's reference in a private field or property in FormA class, so
that the FormA can retrieve FormB's reference in later time.

This is not possible because we do not know when de user will open de
FormA/B nor in which order.
If you did not store the FormB's reference during creation, you have to
enumerate through the child form collection and get the "FormB" reference
like below:

System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form^ mdiparent=this->MdiParent;

FormB^ formB;
for(int i=0;i<mdiparent->MdiChildren->Length;i++)
{
if(mdiparent->MdiChildren->GetType()->FullName->Contains("FormB"))
{
formB=dynamic_cast<FormB^>(mdiparent->MdiChildren);
}
}
if(formB!=nullptr)
{
MessageBox::Show(formB->Handle::get().ToString());
}
}


All this idea works for me, and at the end I have adopted this solution
if(mdiparent->MdiChildren->GetType() == formB::typeid)

Ben Voigt does not recommend to use strings, is less efficient?

Thanks for your help.
 
B

Ben Voigt

Tomas said:
Based on my understanding, your FormA and FormB are both the child forms
of
the main MDI window. Now, you want to obtain the C++/CLI reference to the
FormB if opened. If I have misunderstood you, please feel free to tell
me,
thanks.

Yes this is the situation.
Normally, when we are creating the FormB instance in the code, we'd
better
store FormB's reference in a private field or property in FormA class, so
that the FormA can retrieve FormB's reference in later time.

This is not possible because we do not know when de user will open de
FormA/B nor in which order.
If you did not store the FormB's reference during creation, you have to
enumerate through the child form collection and get the "FormB" reference
like below:

System::Void button1_Click(System::Object^ sender, System::EventArgs^
e) {
Form^ mdiparent=this->MdiParent;

FormB^ formB;
for(int i=0;i<mdiparent->MdiChildren->Length;i++)
{
if(mdiparent->MdiChildren->GetType()->FullName->Contains("FormB"))
{
formB=dynamic_cast<FormB^>(mdiparent->MdiChildren);
}
}
if(formB!=nullptr)
{
MessageBox::Show(formB->Handle::get().ToString());
}
}


All this idea works for me, and at the end I have adopted this solution
if(mdiparent->MdiChildren->GetType() == formB::typeid)

Ben Voigt does not recommend to use strings, is less efficient?


The version with typeid just does a comparison on the v-table ptr, very
efficient. It is a special case in the JIT. Not only is any string
comparison a little more expensive, but it requires getting the class name
from the metadata and all metadata access is extremely expensive. The other
problem is that the string comparison isn't even correct! It would match
any FormB in any namespace and any assembly, vs FormB::typeid which is
resolved at compile-time to your FormB class.

Ultimately, though, you don't need the first check at all, because
dynamic_cast returns null if the object is not the type you cast to.
 
J

Jeffrey Tan[MSFT]

Hi Ben,

Oh, yes. Actually, my code snippet mainly focuses on the logic of the
Tomas' problem. Anyway, thanks for your sharing. Your suggestion is more
accurate in this scenario. It is useful to us :).

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top