我无法将值传递给其他表单类

时间:2016-02-24 03:35:05

标签: vb.net

我正在使用Visual Basic 2005 ...我一直试图将值传递给其他表单类但它不起作用。

这是我在frmLogin表单中的代码:

    Imports System.Data.Odbc

Public Class frmLogIn
Dim Myconn As New OdbcConnection("Dsn=MS Access Database;dbq=D:\GRADING\dbTrip.mdb;defaultdir=D:\GRADING;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin")
    Dim MyForm As New frmLimited
    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        Dim MyDSUser As New DataSet
        Dim MyDAUser As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblAccount WHERE Username = '" & Me.txtUser.Text & "'", Myconn)

        If MyDAUser.Fill(MyDSUser, "tblAccount") = 0 Then
            MessageBox.Show("No Account Found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)

        Else
            If MyDSUser.Tables("tblAccount").Rows(0)("password").ToString = Me.txtPass.Text Then
                Me.btnLogin.Enabled = True
                Dim varID As Integer = MyDSUser.Tables("tblAccount").Rows(0)("ID")
                Dim MyDSRoles As New DataSet
                Dim MyDARoles As OdbcDataAdapter = New OdbcDataAdapter("SELECT * FROM tblRoles WHERE ID = " & varID & " ", Myconn)

                MyDARoles.Fill(MyDSRoles, "tblRoles")
                If MyDSRoles.Tables("tblRoles").Rows(0)("Role").ToString = "Administrator" Then
                    Me.Hide()
                    frmAdmin.Show()
                Else
                    MyForm.MyVar = varID
                    frmLimited.Show()

                End If

            Else
                MessageBox.Show("Password Mismatch", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            End If

        End If

    End Sub
End Class

这里的代码是frmLimited形式:

Imports System.Data.Odbc

Public Class frmLimited
Dim Myconn As New OdbcConnection("Dsn=MS Access Database;dbq=D:\GRADING\dbTrip.mdb;defaultdir=D:\GRADING;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin")
    Public MyVar As Integer
Private Sub frmLimited_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lblName.Text = MyVar

    End Sub
End Class

frmLogin中varID的值是我试图传递给MyVar中名为frmLimited的另一个已声明为Public的形式。我不认为Imports System.Data.Odbc会影响整个代码。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

这里的问题非常简单,关于这三行:

Dim MyForm As New frmLimited

MyForm.MyVar = varID
frmLimited.Show()

您的代码创建了frmLimited的实例,更改了属于创建的实例(MyForm)的变量MyVar的值,然后尝试show原始的表单frmLimited,而不是您创建的实例MyForm

因此,您只需要更改以下行:

frmLimited.Show()

有了这个

MyForm.Show()

问题解决了:))

希望这有帮助

答案 1 :(得分:-1)

你可以在frmLimited上为变量创建一个属性,并从MyForm传递它:

frmLimited

Public Class frmLimited
Private _myVar as Integer
Public Property MyVar as Integer
' Get & Set
End Property

Dim Myconn As New OdbcConnection("Dsn=MS Access Database;dbq=D:\GRADING\dbTrip.mdb;defaultdir=D:\GRADING;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin")
Private Sub frmLimited_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lblName.Text = _myVar

    End Sub
End Class

MyForm的

 If MyDSRoles.Tables("tblRoles").Rows(0)("Role").ToString = "Administrator" Then
                    Me.Hide()
                    frmAdmin.Show()
                Else
                    frmLimited.MyVar = varID '<--
                    frmLimited.Show()

                End If