Solved

SetParent and UserControls in VB6

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

734 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