VB.NET 在表单之间传递数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13938530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:35:40  来源:igfitidea点击:

VB.NET Pass Data Between Forms

vb.net

提问by Wanny Miarelli

Frist of all sorry for my bad english.

首先很抱歉我的英语不好。

I'm making an application in VB.NET and I have a little problem. I have two forms one main FORM (always open), and one that I open as Dialog.

我正在 VB.NET 中创建一个应用程序,但我遇到了一个小问题。我有两种形式,一种是主形式(始终打开),另一种是作为对话框打开的形式。

In the secondary form (such as the open dialog) I can choose what to do and based on what I choose I have to trigger an event in the main form. Let me explain, In the child form I choose the customer number 2, I press OK, and the main form has to load all the data related to the customer number 2.

在辅助表单(例如打开的对话框)中,我可以选择要执行的操作,并且根据我的选择,我必须在主表单中触发一个事件。让我解释一下,在子窗体中,我选择了客户编号 2,然后按 OK,主窗体必须加载与客户编号 2 相关的所有数据。

Obviously, being object-oriented vb.net I can not call a sub from another form (because I do not have access to the instance) and I can not declare a new one, because the main form is always open.

显然,作为面向对象的 vb.net 我不能从另一种形式调用子(因为我无权访问实例)并且我不能声明一个新的,因为主窗体总是打开的。

How do I then pass the id of the customer and raise the event to load?

然后我如何传递客户的 id 并引发事件以加载?

回答by

Expose the customer ID in the child through a Public or Friend property, for example (child form):

通过 Public 或 Friend 属性在 child 中暴露客户 ID,例如(子表单):

Public Property CustomerID as Integer

Private Sub OK_Click(s as Object, e as eventargs) Handles OK.Click
    CustomerID = id 'pass the value here
    Me.DialogResult = DialogResult.Ok
End Sub

On the main form then:

在主窗体上:

If frmChild.ShowDialog = DialogResult.Ok Then
    MessageBox.Show("Customer ID: " + frmChild.CustomerID)
End If