Pergunta

Basically, I have a custom child form class which has events that will be passed to the parent. In the custom child form, I have a declaration of a "MustInherit" class that inherits the DevExpress User Control Class.

The reason for this, is I have many user controls that derive from this base class, and the child form can have an instance of any one of these controls, and doesnt care which. The only requirement is that the child form can handle the same events from each type of control the same way.

Some watered down code snippets(still pretty long unfortunately):

'''Inherited Class
Public Class ChildControlInheritedClass
    'A Button Click event that starts the chain of events.
    Private Sub btnMoveDocker_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertToTab.Click
        OnMoveToDocker(Me, New ChildGridMoveArgs(Me))
    End Sub
End Class

'''Base Class
Public MustInherit Class ChildControlBaseClass
    Inherits DevExpress.XtraEditors.XtraUserControl
    Public Class ChildGridMoveArgs
        Inherits System.EventArgs
        Public Sub New(ByVal _ChildControl As ChildControlInheritedClass)
            ChildControl = _ChildControl
        End Sub
        Public ChildControl As ChildControlInheritedClass
    End Class
    Public Event MoveToDocker(ByVal sender As Object, ByVal e As ChildGridMoveArgs)
    Protected Overridable Sub OnMoveToDocker(ByVal sender As Object, ByVal e As ChildGridMoveArgs)
        '''Once this RaiseEvent is fired, nothing happens. The child form is oblivious.
        RaiseEvent MoveToDocker(sender, e)
    End Sub
End Class

'''Child Form Class
Public Class ChildForm
    Private WithEvents cgChild As ChildControlBaseClass
    Public Property ChildGrid() As ChildControlInheritedClass
        Get
            Return cgChild
        End Get
        Set(ByVal value As ChildControlInheritedClass)
            RemoveHandler cgChild.MoveToDocker, AddressOf cgChild_MoveToDocker
            cgChild.Dispose()
            cgChild = Nothing
            cgChild = value
            AddHandler cgChild.MoveToDocker, AddressOf cgChild_MoveToDocker
        End Set
    End Property
    Public Event MoveToDocker(ByVal sender As Object, ByVal e As ChildControlInheritedClass.ChildGridMoveArgs)
    Public Sub cgChild_MoveToDocker(ByVal sender As Object, ByVal e As ChildControlInheritedClass.ChildGridMoveArgs)
        RaiseEvent MoveToDocker(sender, New ChildControlInheritedClass.ChildGridMoveArgs(cgChild))
    End Sub
End Class

Public Class frmMain
    Private Sub OpenNewWindow()
        Dim frm As New ChildForm
        Dim chld As New ChildControlInheritedClass
        frm.ChildGrid = chld
        frm.Show()
    End Sub
End Class

In a nutshell, thats how I made the child form and how everything is suppose to work. But when I press the button in the inherited child control, the event only gets as far as the base class and never traverses the RaiseEvent into the child form thats suppose to handle the event.

Am I even in the ballpark here?

Thanks for reading!

Foi útil?

Solução

You forgot to add your event handle by using AddHandler or Handles identifier. See below using the Handles cgChild.MoveToDocker identifier.


Public Class ChildForm
    ...
    Public Sub cgChild_MoveToDocker(ByVal sender As Object, ByVal e As ChildControlInheritedClass.ChildGridMoveArgs) Handles cgChild.MoveToDocker
        RaiseEvent MoveToDocker(sender, New ChildControlInheritedClass.ChildGridMoveArgs(cgChild))
    End Sub
End Class

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top