Windows Develop Bookmark and Share   
 index > Windows Forms Designer > <URGENT> Calling a parent form method from a child user control
 

<URGENT> Calling a parent form method from a child user control

Howdy all,

I'm new to Windows developement and have been struggling on this subject for sometime now. My query is how do you call a method in a parent form from a user control that is in another project? Someone mentioned referencing the Parent form. So in the parent form, I call the constructor of the user control, 

UserControl(this);

and in the constructor of the UserControl ...

public MainMenu(Form myParentForm)

this works but it's not referencing any of the methods I created. Any help would be much appreciated. 

Cheers
Alvin
MigrationUser 1  Wednesday, July 28, 2004 7:15 PM
because your not casting your form object or passing in your inherited form with your methods, you only have methods from the base class, Form


public MainMenu(Form myParentForm) 



should be


public MainMenu(MyFormClass myParentForm) 
</code
MigrationUser 1  Wednesday, July 28, 2004 9:42 PM
i use VB but hope this will help
and why do you get duble line height in code sampels?
i dont like it at all


Public Class Child
    Inherits System.Windows.Forms.Button


'Region Component Designer Generated Code


    Private ParentForm As Form

    Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
        MyBase.OnParentChanged(e)

        'This won't work...
        'ParentForm = CType(Me.TopLevelControl, Form)
        'AddParentHandlers()

        'This does!
        Call Parent_ParentChanged(Me, e)
    End Sub
    Private Sub Parent_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim c As Control = DirectCast(sender, Control)
        RemoveHandler c.ParentChanged, AddressOf Parent_ParentChanged
        c = c.Parent

        ''If you don't have AndAlso in C# you do:
        'If TypeOf c Is Form Then
        '    If DirectCast(c, Form).TopLevel = True Then
        '        ParentForm = DirectCast(c, Form)
        '        AddParentFormHandlers()
        '    Else 'c is not toplevel
        '        AddHandler c.ParentChanged, AddressOf Parent_ParentChanged
        '    End If
        'Else 'c is not a form
        '    AddHandler c.ParentChanged, AddressOf Parent_ParentChanged
        'End If


        If TypeOf c Is Form AndAlso DirectCast(c, Form).TopLevel = True Then
            ParentForm = DirectCast(c, Form)
            AddParentFormHandlers()
        Else 'c is not the topform
            AddHandler c.ParentChanged, AddressOf Parent_ParentChanged
        End If
    End Sub

    'Here you can add all your event handlers
    Private Sub AddParentFormHandlers()
        AddHandler ParentForm.Click, AddressOf OwnerForm_Click
        'AddHandler ParentForm.MouseMove , AddressOf OwnerForm_MouseMove
        'Etcetera...
    End Sub

    Private Sub OwnerForm_Click(ByVal sender As Object, ByVal e As EventArgs)
        MsgBox("Clicked on ParentForm!")
    End Sub

End Class
MigrationUser 1  Tuesday, January 11, 2005 10:35 PM
Sorry i didnt read your question very good
but i am a dutch kaaskop (cheesehead), i can't read good english

laters!
MigrationUser 1  Tuesday, January 11, 2005 10:38 PM

You can use google to search for other answers

Custom Search

More Threads

• Parent Property
• Whats the best approach to do date AND time?
• Need Image Control With SubImages
• Outlook Style Custom List Control
• rewrite vba to vb.net
• DLL Components in Windows Forms???
• Changed event
• Adding Dataset to component tray
• Form.designer separated from its form file
• Why isn't CellStyle Format preventing alpha characters in Number format column?