模块 vb.net 中的访问控制值



我需要从 VB.net 程序中的模块 1 访问 Form1 中的文本框值。 我想我在某处读到,您可以将控件设为公开,然后您可以访问这些值。 我试过这个,但它不起作用。 我需要做什么?

这是我的代码:

    Module Module1
    Dim connectionMaster = New ConnectionMaster()
    Dim uname As String
    Dim pw As String
    Dim frmInstance As Form2
    frmInstance = New Form2
    uname = frmInstance.Username
    pw = frmInstance.Password

用户名和密码采用 Form2。 当我尝试构建它时,我收到错误消息"'System.Windows.Forms.TextBox'类型的值无法转换为'字符串'"

我将上述内容更改为:

uname = frmInstance.Username.text
pw = frmInstance.Password.text

现在它起作用了!

一种简单的方法是在模块中创建 2 个公共属性。

Private _name As String
Public Property UserName() As String
    Get
        Return _username
    End Get
    Private Set(ByVal value As String)
        _username = value
    End Set
End Property

然后在表单中,在调用模块时,只需将值传递给模块。

最新更新