Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Easy SetParent question

Posted on 2004-04-20
2
Medium Priority
?
480 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 100 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no 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.
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

604 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