比较来自世界各地的卖家的域名和 IT 服务价格

如何从子公司造成父母的表格活动?

我有一个父形式,我从中创建和另一个表单的实例。 /女儿形态/

我正在尝试从子公司拨打父母形式的活动。
/我试图阅读一些类似的问题 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
已邀请:

郭文康

赞同来自:

从类外部运行事件取决于执行事件和您的类。

例如,在更改属性值时发生特定案例时,会发生事件,当您按下按钮等时。 要调用此事件,您必须模仿此事件,例如,必须设置要启动其更改事件的属性的值,或者例如,对于按钮,您必须调用方法
PerformClick

这个按钮。

但总的来说,最好在父母形式中创建一个开放方法,将逻辑放在那里并导致这种方法从表单的外部。

例如,我猜你需要召唤一个事件。
Click

纽扣 1 并有这段代码:


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//


样本

这是一个含有样品 2 形式:


'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

三叔

赞同来自:

在子项中添加一个公共事件,并在主表单中调用其处理程序中的过程。 另外,没有什么可以在拾取时传输事件 - 坏主意。 您必须将我作为发件人的对象,以及一个新对象 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

要回复问题请先登录注册