473,394 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Handle to an opened form

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.

Feb 25 '07 #1
6 1695
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 26 '07 #2
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"))
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]);
}
}
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Feb 26 '07 #3
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[i]->GetType()->FullName->Contains("FormB"))
{
formB=dynamic_cast<FormB^>(mdiparent->MdiChildren[i]);
}
}
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[i]->GetType() == formB::typeid)

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

Thanks for your help.
Feb 26 '07 #4

"Tomas" <To***@newsgroup.nospamwrote in message
news:DE**********************************@microsof t.com...
>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[i]->GetType()->FullName->Contains("FormB"))
{
formB=dynamic_cast<FormB^>(mdiparent->MdiChildren[i]);
}
}
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[i]->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.
>
Thanks for your help.

Feb 26 '07 #5
Thanks for your explanation.
Feb 26 '07 #6
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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 27 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: NeverLift | last post by:
But, if it's not open, I don't want to open it . . . using window.open will open it if it doesn't exist, even if the url in that open is null (the window is then empty -- but it's open). The...
3
by: Douglas Buchanan | last post by:
Buttons don't work if form is opened on startup A2k If 'frmMain' is set to open by default at startup none of the buttons work. If 'frmMain' is opened from the database window then all the...
3
by: Daniel H. | last post by:
Hi! When I create a new form programatically, I can save its handle in a collection of some type Later, I need to find that form based on a handle I saved and call one of its functions. The newly...
12
by: Daniel Walzenbach | last post by:
Hi, I have a Website which allows users to input data. After they finished entering data they can click a button to save their input. Problem now is, that I have no possibility to visualize that...
1
by: RJN | last post by:
Hi In the mainscreen I have a datagrid and from here I open new windows on click of link button. And I'm doing it through RegisterStartupScript Me.RegisterStartupScript("PopUp", _ "<script...
1
by: Daniel | last post by:
does the windows file handle change? are file handles unique to the whole operating system or just the current directoy? if a file is opened then closed then opened again, does the file handle...
5
by: RichG | last post by:
I'm looking for a way to bring an open form to the top. I know that is I open a form directly with form1.show() and then later, while the form is open I do another form1.show(), that I will get...
2
by: Jonathan Boivin | last post by:
Hi people, Let me introduce to how I get this error. I have a form which load all my bills representation depending upon filters which each bill is a usercontrol of my own having some...
0
by: Sasie7679 | last post by:
Dear All, We have a requirement to open any document with a specific application and display it within a OCX control (Example - word, excel, adobe reader, MSPaint, Photoshop etc). We are using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.