none
how to set parent in vb.net RRS feed

  • Question

  • i am working on window application in vb.net.

    i have a  form and i want to open it as a child form of microsoft visio application.this application is already open

    i use showdialog() but it doesnot work and may be i used wrong way, i also used set parent API and also the same problem, its not working

    if someone know please help !

    thanks

    Thursday, April 6, 2006 12:40 PM

All replies

  • Hi Bony,
    If you want to open any form as child form. then you have to specify a parent form to it

    such as given below in vb.net

    for displaying as child form you need a MDIform

    Dim childform as Form
    childform.MdiParent = MDIformName
    
    childform.WindowState = FormWindowState.Maximized
    
    childform.Show()



    This way a childform will open in a Mdiform

    please try this

    If I am making any mistake, Please let me know and
    please specify your problem more clearely

    ATishRG



    atishrg
    • Proposed as answer by AtishRG Monday, July 28, 2008 11:59 AM
    • Edited by Paul IshakModerator Wednesday, February 20, 2013 5:37 PM Please use MSDN's code insertion feature
    Monday, July 28, 2008 11:58 AM
  • You want a VB form to be contained within Visio? I would think your best bet would be to write your application within the Visio development environment. Would this not work?

    Stephen J Whiteley
    Monday, July 28, 2008 2:28 PM
    Moderator
  • Dim frm As New page1
    
    frm.MdiParent = Me
    
    frm.Show()


     

    page1 is show the parent of 'me' form

    you can write name of Me form


    • Edited by Paul IshakModerator Wednesday, February 20, 2013 5:36 PM Please use MSDN's code insertion feature.
    Tuesday, April 27, 2010 5:48 PM
  • For VB.Net 2010

    Imports System.Runtime.InteropServices
    Public Class YourClassName
        Inherits System.Windows.Forms.Form
        <DllImport("user32")> Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        End Function
    
        Private Sub ParentForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  SetParent(ChildForm.Handle, Me.Handle)
    
    End Sub
    
    [/CODE]
    
    For VB6
    
    [CODE]
    
    Const WS_CHILDWINDOW = &H40000000
    Public Const GWL_STYLE = (-16)
    Public Const WS_CHILD = &H40000000
    Public Const WM_CLOSE = &H10
    
    Public Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
    
    Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
       (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    
    Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
       (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Function SetParentChild(Form2 As Form, Form1 As Form)
    
     Dim xHwnd As Long
     Dim yHwnd As Long
     Dim ctl As Control
     Dim Text11 As TextBox
    ' MaintFlagon = False
     yHwnd = 0
     xHwnd = SetParent(Form2.hWnd, Form1.hWnd)
      
      xHwnd = GetWindowLong(Form2.hWnd, GWL_STYLE)
      On Error Resume Next '''''''''''''''''''''For Error with 'yHwnd' whose value exceeds limit at times
       yHwnd = xHwnd + WS_CHILDWINDOW
       yHwnd = 0
       yHwnd = xHwnd + WS_CHILDWINDOW
      xHwnd = SetWindowLong(Form2.hWnd, GWL_STYLE, yHwnd)
      Form2.Left = (Form1.Width - Form2.Width) / 2 'To centre horizontally
      Form2.Top = (Form1.Height - Form2.Height) / 2 'To centre vertically 
      
    End Function


    Both above codes are funtional.


    • Proposed as answer by Rocky_Sharma Tuesday, February 12, 2013 3:05 PM
    • Edited by Paul IshakModerator Wednesday, February 20, 2013 5:35 PM Rocky, Please use MSDN's code insertion button, second button from the right, thanks.
    Tuesday, February 12, 2013 3:04 PM
  •  open it as a child form of microsoft visio application.

    This sounds like a Visio question, not a Visual Basic question.


    “If you want something you've never had, you need to do something you've never done.”

    Don't forget to mark helpful posts and answers ! Answer an interesting question? Write a new article about it! My Articles
    *This post does not reflect the opinion of Microsoft, or its employees.

    Wednesday, February 20, 2013 5:39 PM
    Moderator
  • Here is a working Code that can be used as a Public Function in a Public Module

    'Long
        Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
        Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Long) As Long
    
        'Integer
        Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As Integer) As Integer
        Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
    
        Const WS_CHILDWINDOW As Integer = &H40000000
        Public Const GWL_STYLE As Short = (-16)
        Public Const WS_CHILD As Integer = &H40000000
        Public Const WM_CLOSE As Integer = &H10
    
        Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
        Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer
    
        Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
        Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Integer, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
        Public Declare Function GetParent Lib "user32" (ByVal hWnd As Integer) As Integer
        Public Declare Function SetParent Lib "user32" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    
    
    Public Function SetParentChild(ByRef Form2 As System.Windows.Forms.Form, ByRef Form1 As System.Windows.Forms.Form) As Object
    
            Dim Rect As Rectangle
            Dim MaintFlagon As Object
            Dim CatComboTxtCallingFrm As Object
            Dim xHwnd As Integer = 0
            Dim yHwnd As Integer = 0
            Dim ctl As System.Windows.Forms.Control
            Dim Text11 As System.Windows.Forms.TextBox
            yHwnd = 0 'This is essential otherwise yHwnd value will go beyond limits causing error
            xHwnd = SetParent(Form2.Handle.ToInt32, Form1.Handle.ToInt32)
            xHwnd = GetWindowLong(Form2.Handle.ToInt32, GWL_STYLE)
            On Error Resume Next 'For Error with 'yHwnd' whose value exceeds Limit at times
            yHwnd = xHwnd + WS_CHILDWINDOW
            yHwnd = 0
            yHwnd = xHwnd + WS_CHILDWINDOW
            xHwnd = SetWindowLong(Form2.Handle.ToInt32, GWL_STYLE, yHwnd)
            '=====================
            If Form1 IsNot Nothing Then
                Rect = Form1.RectangleToScreen(Form1.ClientRectangle)
            Else
                Rect = Screen.FromPoint(Form2.Location).WorkingArea
            End If
            'This is for Centering the Child Form
            Dim x = Rect.Left + (Rect.Width - Form2.Width) \ 2
            Dim y = Rect.Top + (Rect.Height - Form2.Height) \ 2
            Form2.Location = New Point(x, y)
          
        End Function

    • Proposed as answer by RakLali Monday, July 20, 2015 4:43 PM
    Monday, July 20, 2015 3:18 PM
  • Dear Mr Paul, That was my first time ever. So I didn't know. I have done it better this time. See the next Post.
    Monday, July 20, 2015 3:21 PM