vba 将变量从子级传递给父级,就像函数 VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10712741/
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-11 16:15:16  来源:igfitidea点击:

Pass variable From child to parent, Like a Function VB.NET

vb.netformsvbaparent

提问by John Nu?ez

I have created 2 forms (Parent & Child), i want to store the unique textbox value to a variable into the parent form.

我创建了 2 个表单(父和子),我想将唯一的文本框值存储到父表单中的变量中。

Also like this:

也像这样:

Parent Code:

父代码:

dim passed_value = new childform()
passed_value.show()

On close:

关闭时:

refresh passed_value variable using childform textbox value.

使用子表单文本框值刷新passed_value 变量。

回答by Writwick

You can Do the Following to accomplish the Task :

您可以执行以下操作来完成任务:

  • Declare a Stringvariable in the child form.
  • String在子窗体中声明一个变量。
    Public value As String
  • Use ShowDialog()in the Main Form to show the Child form.
  • 使用ShowDialog()在主窗体显示子窗体。
    Dim frm As New Form2
    frm.ShowDialog()
  • [Set the valuein your form as per your needs]
  • [value根据您的需要在您的表格中设置]
    value = "New Value"
  • Now get the valuevariable from the Child form and set the textbox text according to it.
  • 现在value从 Child 表单中获取变量并根据它设置文本框文本。
    TextBox1.Text = frm.value

回答by Yatrix

I would make it a property and give it the necessary access, which to me looks to be ReadOnly.

我会将其设为属性并为其提供必要的访问权限,在我看来,它是ReadOnly.

Private _myValue As DataType

Public ReadOnly Property MyValue() As DataType
Get
    Return _myValue
End Get

You could access it as such: myForm.MyValue.

您可以这样访问它:myForm.MyValue.