Solved

SetParent and UserControls in VB6

Posted on 2002-05-30
3
1,747 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

823 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