Specifying Parent to a Windows Form Where Parent Form is in VB 6

G

Guest

I have built a class library in Visual Basic .NET 2005 that exposes a class
as a COM class using the ComClass attribute. This COM class allows (among
other things) loading a Windows Form. I am using this .NET class library from
Visual Basic 6 as if it were a COM DLL using the new features of Visual
Studio 2005. The Visual Basic 6 application contains an MDI form. I can load
the Windows Form contained in the .NET class library from Visual Basic 6 by
means of the COM class exposed. This works fine as long as the Windows Form
is loaded as a non-child form. The problem is that I want the Windows Form to
be loaded as a child form of the Visual Basic 6 MDI form. How can I specify
to the .NET Windows Form that the parent form is a form in Visual Basic 6?

Thanks in advance!
 
L

Linda Liu [MSFT]

Hi,

Thank you for posting.

This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
P

Peter Huang [MSFT]

Hi Jairo,

Thanks for your posting!
Based on my understanding, you want to create a .NET form via COM Interop
and use that form in VB6. That is OK.
But when you try to set the .NET form as a children form of a VB6 MDI form,
you will get error.

If I have any misunderstanding, please feel free to post there.

Based on my research, this is by design. Because VB6 Form is different from
.NET form, we can consider them as different class. While the VB6 MDI
parent wants a vb6 form, it gets a .NET form which will cause unexpected
error.

Also we may use Win32 API to force the .NET form as a children of VB6 MDI
parent Form, but that will break the internal relation VB6 maintain for its
own VB6 form.
So this behavior is not supported.

Here I just wonder why you need to do so.

I suggest you upgrade your VB6 project to VB.NET project so all the code
will be pure .NET.

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi,

I have done some research on this issue. Unfortunately, I haven't found a
workaround for the problem yet.

I set up a class library project called TestComClass in VS2005 and added a
COM Class item named ComClass1 into the project. I added a public method
CreateForm1() in the ComClass1. The following code shows the method.

Public Function CreateForm1() As Form1
Dim form As New Form1
Return form
End Function

I compiled the class library to generate a COM. Then I create a simple exe
project in VB6 to use the COM. I found the following facts.

1. If I want to open a form as a MDIChild in a MDIParent form in vb6 , I
should set the MDIChild property of the form to True in design-time. The
MDIChild property of the form can not be set in run-time.

2. I added a method to set the MDIParent for the form in .Net in the Com
Class. The following code shows the method.
Public Sub SetMDIParent(ByRef childfrm As System.Windows.Forms.Form,
ByRef parentfrm As System.Windows.Forms.Form)
childfrm.MdiParent = parentfrm
End Sub
Then I call the SetMDIParent() method in vb6. The following code is in vb6.

Dim com As New TestComClass.ComClass1
Dim frm As TestComClass.Form1
Set frm = com.CreateForm1
com.SetMDIParent frm, Me ' an error will rise here, saying "Type mismatch"
frm.Show

When running, an error rose saying "Type mismatch" with the sentence
"com.SetMDIParent frm, Me".
I will go on researching on this issue. I appreciate your patience.




Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi,

Thanks for your post.

What you outline with the SetMDIParent() method is precisely the problem I
am trying to overcome. I will appreciate any lights on this.

Thanks in advance!

Regards,

Jairo
 
G

Guest

Hi Peter,

Thanks for your posting!

Your understanding of my post is right on!

I undertand the mechanism behind VB6, VB.NET and the .NET Interop and I
completely agree with you. I also understand the consequences of breaking the
internal relation of VB6 with its forms in case the Win32 API is used. The
best option as you suggest is to upgrade the entire VB6 project to .NET but
that is not going to be possible for our case. Our main goal will be to have
everything in .NET but in the meantime we would like to take advantage of the
interop to put new things in .NET and do the conversion bit by bit. This
scenario will prompt situations like the one I am trying to overcome
considering its limitations.

I am still interested in knowing how to use the Win32 API to force the .NET
form as a children of VB6 MDI (assuming the corresponding consequences and
limitations).

Thanks in advance!

Jairo
parent Form
 
P

Peter Huang [MSFT]

Hi Jairo,

Thanks for your quickly reply!
The Win32 API approach I mention before is to call the SetParent API.
SetParent Function
The SetParent function changes the parent window of the specified child
window.
So that we force the Winform's parent window to be the VB6 MDI form.
But that may cause unexpected behavior and is not supported, which is a
hack way. I did not recommend you do that way.

"Our main goal will be to have
everything in .NET but in the meantime we would like to take advantage of
the
interop to put new things in .NET and do the conversion bit by bit. This
scenario will prompt situations like the one I am trying to overcome
considering its limitations."

From your description, I understand you means to have everything in VB6 but
utilized .NET feature via Com Interop.
If I misunderstood, please let me know.

So far I think one way is to keep using UI related code in VB6 but use .NET
class library without UI to leverage .NET power.
Or use all the UI related code in .NET, but in that way, why not upgrade
all the code to .NET.

If you still have any concern please feel free to post here.



Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu [MSFT]

Hi Jairo,

Thank you for your concern.

Since we couldn't set a form as a MDIClient in run-time, and it's
impossible to set a form in VS2005 as a MDIClient form by calling the
SetMDIParent() method in the Com Class, the only way to solve this problem
is to use SetParent API function as Peter has described.

Unfortunately, even if you use the SetParent function, the form in VS2005
couldn't become a real MDIClient form. The form couldn't receive any
messages that the MDIParent form sends to it. There's a lot of work to do.
And the work is in a hack way which is not supported.

Maybe you could have a try not getting a winform from the COM Class and
instead use non-UI function.

If you have any concerns or need anything else, please feel free to let me
know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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