Solved

Easy SetParent question

Posted on 2004-04-20
2
466 Views
Last Modified: 2008-03-04
I've done this before, but I can't remember how I got it working (and why it won't now). I want to set a form as a child of another form. I know I need to use the SetParent API, but it's like it doesn't see that line. My project is set up with frmMain and frmChannel. Here's the frmMain code:

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
' The standard stuff goes here, taken out to save space
#End Region

    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim objTest As New frmChannel
        SetParent(objTest.Handle.ToInt32, Me.Handle.ToInt32)
        objTest.Show()
    End Sub
End Class

According to http://www.allapi.net/ SetParent returns nonzero when it succeeds. When I used MsgBox to see what the output was, it was a really huge number (nonzero) but it didn't get set as a child. What am I doing wrong?

I'm using VB.NET 2003 in case you're wondering.
0
Comment
Question by:todd-
[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
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 25 total points
ID: 10873443
Your API declaration was for VB6.  Try this out instead:

Public Class Form1
    Inherits System.Windows.Forms.Form

    ' Region " Windows Form Designer generated code "

    Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f As New Form2
        SetParent(f.Handle, Me.Handle)
        f.Show()
    End Sub
End Class

Regards,

Idle_Mind
0
 
LVL 1

Author Comment

by:todd-
ID: 10873536
Very nice, thanks a lot :)
It's odd though, I remember last time copying and pasting the API straight from allapi.net and to get it to work together I just had to use .ToInt32.

Whatever, thanks all the same :D
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

707 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