Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2007

Tip: Looking for answers? Try searching our database.

Handle to an opened form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tomas - 25 Feb 2007 08:32 GMT
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.
Jeffrey Tan[MSFT] - 26 Feb 2007 06:59 GMT
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[i]->GetType()->FullName->Contains("FormB"))
        {
            formB=dynamic_cast<FormB^>(mdiparent->MdiChildren[i]);
        }
    }
    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.
Ben Voigt - 26 Feb 2007 14:22 GMT
> 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
[quoted text clipped - 8 lines]
> {
> if(mdiparent->MdiChildren[i]->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[i]);
[quoted text clipped - 30 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Tomas - 26 Feb 2007 16:52 GMT
> 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
[quoted text clipped - 16 lines]
>     }
>  }

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

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

Thanks for your help.
Ben Voigt - 26 Feb 2007 17:17 GMT
>> Based on my understanding, your FormA and FormB are both the child forms
>> of
[quoted text clipped - 39 lines]
>
> 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.

> Thanks for your help.
Tomas - 26 Feb 2007 17:32 GMT
Thanks for your explanation.
Jeffrey Tan[MSFT] - 27 Feb 2007 03:15 GMT
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.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.