Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SetParent and UserControls in VB6

Posted on 2002-05-30
3
Medium Priority
?
1,807 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 800 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

722 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