Solved

Easy SetParent question

Posted on 2004-04-20
2
435 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-
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 25 total points
Comment Utility
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-
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now