Solved

SetParent and UserControls in VB6

Posted on 2002-05-30
3
1,764 Views
Last Modified: 2012-06-22
I have a user control on a form.  The only control on the usercontrol is a second usercontrol.

At run time, I want to run various initialization code in the first control to set up the second one and then set the parent of the second on to be the form rather than the first user control so that I can drag it around the entire form with out being limited to the boundaries of the first control, which would now be made invisible.

If I can get this to work, I can make a nice, tidy encapsulated module.

What happens when I use the setparent api using the hwnd of the second (inner) control and the hwnd of the form is the second control moves to the top of the first control but is still contained by it.

I added a second form in an attempt to see what was happending.  Assigning the hwnd of the inner control to the second form MOVED BOTH CONTROLS to the second form.

Then I tried adding a form inside of the usercontrol project and placed the inner control on it.  Now when I try to set the parent of the innercontrol, the entire form it is on comes with it.

Either of the above techniques work if I try to move a textbox.  It appears to be a "problem" specific to a UserControl.

I know this is a confusing description of the problem.  Any help is appreciated.

In case anyone asks, I'm getting the usercontrols hwnd by adding a public property to the user control which returns UserControl.ContainerhWnd.
0
Comment
Question by:bob_online
3 Comments
 
LVL 1

Expert Comment

by:alaplume
ID: 7044645
I don't think you can do that.
0
 
LVL 6

Accepted Solution

by:
pierrecampe earned 200 total points
ID: 7045358
I dont know if i did understand what you want however the following seems to work:
a usercontrol2 inside a usercontrol1

usercontrol2 code:---------------------------
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Private Const GWL_HWNDPARENT = (-8)

Private Sub UserControl_Show()
 If Ambient.UserMode Then
    a = SetParent(UserControl.hWnd, GetParent(UserControl.ContainerHwnd))
    'a = SetWindowLong(UserControl.hWnd, GWL_HWNDPARENT, GetParent(UserControl.ContainerHwnd))
 End If
End Sub

usercontrol1 code:--------------------------------
Public Sub MoveTo(a, b)
    UserControl21.Move a, b
End Sub

form code:------------------------------
Private Sub Command1_Click()
    UserControl11.Visible = False
End Sub

Private Sub Command2_Click()
    UserControl11.MoveTo 0, 0
End Sub
0
 
LVL 5

Author Comment

by:bob_online
ID: 7046865
That appears to be perfect.  The MoveTo 0, 0 was a side effect, not what I was trying to achieve, but the rest of it is great.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question