如何从子表单触发父表单事件?

时间:2015-10-28 04:54:33

标签: .net vb.net forms winforms events

我有一个父表单,我从中创建另一个表单的实例。(子表单)

我正在尝试从子表单触发父表单上的事件。 (我曾尝试在SO上阅读一些类似的问题,但他们要么在c#中,要么对像我这样的菜鸟的解释太少。

父表:

Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

            Dim index As Integer = Convert.ToInt32(DataGridView1.CurrentCell.RowIndex)

            Dim frmjvauth As New frmjventry_Auth
            frmjvauth.Show()
            frmjvauth.txtjvnumver.Text = DataGridView1.CurrentRow.Cells("jvnumber").Value
            frmjvauth.showjv()
    End Sub

Private Sub child_Change(sender As Object, e As EventArgs)
        MsgBox("hey")
    End Sub

儿童表格:

Private Sub frmjventry_Auth_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
    RaiseEvent child_Change(Nothing, Nothing)
End Sub

2 个答案:

答案 0 :(得分:1)

从班级外部触发活动取决于您的活动和班级的实施 特定情况发生时事件引发,例如更改属性值,单击按钮等时。要触发该事件,您应该模拟发生的事件,例如,您应该为要触发其更改事件的属性设置值,或者例如对于按钮,您应该调用该按钮的PerformClick方法。

但总的来说,最好在父表单中创建一个公共方法,然后将逻辑放在那里,并从表单外部调用该方法。

例如,我想你需要触发按钮1的Click事件并拥有以下代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    //Some logic here
End Sub

选项1

您可以添加公共方法并在那里移动逻辑,并在button1_Click中调用该方法:

Public Sub MyLogic()
    //Some logic here
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MyLogic()
End Sub

然后从外面,你可以这样简单地使用逻辑:

//I suppose f is an instance of your form
f.MyLogic()

选项2

您可以将Button1公开,转到设计师并选择Button1并将Modifier属性的值设置为public。然后,您可以在表单外部使用此代码:

//I suppose f is an instance of your form
f.Button1.PerformClick()

选项3

对于您的手工制作的活动,您可以在表单中创建一个公共方法来引发您的活动:

Public Event YourEvent()

Public Sub OnYourEvent()
    RaiseEvent YourEvent()
End Sub

然后以这种方式在形式之外使用它:

//I suppose f is an instance of your form
f.OnYourEvent()

<强>示例

以下是包含两种形式的示例:

'Here is the code for parent form
Public Class ParentFormClass
    Public Sub MyLogic()
        MessageBox.Show("My Logic is Running!")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MyLogic()
    End Sub    

    'This is the way that you pass an instance of parent to child
    Private Sub ShowChildFormButton_Click(sender As Object, e As EventArgs) Handles ShowChildFormButton.Click
        Dim f As New ChildFormClass(Me)
    End Sub
End Class

'Here is the code for child form
Public Class ChildFormClass
    Private MyParentForm As ParentFormClass

    Public Sub New(form As ParentFormClass)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        MyParentForm = form
    End Sub

    Private Sub CallParentFormMethodButton_Click(sender As Object, e As EventArgs) Handles CallParentFormMethodButton.Click
        MyParentForm.MyLogic()
    End Sub 
End Class

答案 1 :(得分:0)

向子表单添加公共事件,并在主表单的处理程序中调用例程。在筹集活动时也没有通过Nothing是一个坏主意。您应该将Me作为发送方对象传递,将新的EventArgs对象作为e。

传递
Public Class ChildForm
    Public Event OnChanged(ByVal sender As Object, ByVal e As EventArgs)

    Private Sub frmjventry_Auth_FormClosed(sender As Object, e As EventArgs) Handles frmjventry_Auth.FormClosed
        RaiseEvent OnChanged(Me, New EventArgs)
    End Sub
End Class

Public Class MainForm
    Private Sub ChildForm_OnChanged(sender As Object, e As EventArgs) Handles ChildForm.OnChanged
        DoSomething()
    End Sub

    Private Sub DoSomething()
        ...
    End Sub
End Class