Solved

SetParent and UserControls in VB6

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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
Course of the Month8 days, 5 hours left to enroll

617 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