VB.Net 将值传递给另一种形式

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

VB.Net Passing values to another form

vb.netwinformsvisual-studiovisual-studio-2012

提问by Janno

I would like to know how to pass a value from form1 to another form's public sub. The problem is that it says "it is not accesible in this context because it is 'Private'."

我想知道如何将值从 form1 传递到另一个表单的公共子。问题在于它说“在这种情况下它是不可访问的,因为它是'私人'。”

I've tried changing Form 1 Private Sub to Public Sub but the same error remains. How should i make it work?

我尝试将 Form 1 Private Sub 更改为 Public Sub,但仍然存在相同的错误。我应该如何使它工作?

Public Class Form1
Dim test(), text1 As String
Const asd = "abcabc"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    text1 = Space(LOF(1))
    test = Split(text1, asd)
    HOST = test(1)
End Sub

And i want to pass HOST = test(1) value to another form

我想将 HOST = test(1) 值传递给另一种形式

Public Class Form2

Public Sub Check()
    'get the value to here
End Sub

采纳答案by ??ssa P?ngj?rdenlarp

You could pass it as a parameter:

您可以将其作为参数传递:

Public Sub Check(valueToCheck as String)
   'get the value to here
End Sub

Or create a property on form2to receive it:

或者在form2上创建一个属性来接收它:

private _HostOrSomething As String = ""
Friend Property HostOrSomething As String
   Get
        Return _HostOrSomething 
    End Get
    Set(ByVal value As String)
        _HostOrSomething = value
    End Set

In which case, Sub Check could use _HostOrSomething since it is local var. To use these:

在这种情况下,Sub Check 可以使用 _HostOrSomething,因为它是本地变量。要使用这些:

HOST = Test(1)
frm2.Check(HOST)

or

或者

HOST = Test(1)
frm2.HostOrSomething = HOST
frm2.Check

回答by Domnick Toretto

You can use global variables to pass data from one from to another

您可以使用全局变量将数据从一个传递到另一个

        Dim A As New Integer= 10

Here how you declare the global The class can be define anywhere in the application.

这里如何声明全局类可以在应用程序的任何地方定义。

Public Class GlobalVariables
Public Shared INTver As Integer
End Class

And how you use global variable to store the answer is here

以及如何使用全局变量来存储答案在这里

GlobalVariables.INTver= A

put this lines in your "privet sub" and you can access the variable to any of your form that is in your WINDOWS application.

将此行放在您的“privet sub”中,您可以访问该变量到您的 WINDOWS 应用程序中的任何表单。