VB.Net Passing values to another form

試著忘記壹切 提交于 2019-12-17 10:05:04

问题


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'."

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

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

Public Class Form2

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

回答1:


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 form2 to receive it:

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:

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

or

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



回答2:


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.



来源:https://stackoverflow.com/questions/19618668/vb-net-passing-values-to-another-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!