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
|