如何将Public变量从一个表单传递到另一个表单

时间:2016-11-02 21:36:06

标签: .net vb.net forms winforms variables

我试图从一个表单传递2个变量并打开另一个表单,我想使用最后一个表单中的相同变量来查询sql数据库,用基于最后一个表单的信息填充数据

我需要来自此表单的FName和LName由方法Information传递的参数设置。

Public Class UserProfile
Public IMEI As String
Public FName As String
Public LName As String

Public Sub Information(FName As String, LName As String, Dob As String, Phone As String, PhoneType As String, IMEI As String, AppleGoogleID As String, AppleGooglePass As String, Phoneswap As Integer, LockScreen As String)
    Dim UserData As New UserDataInventory
    FName = FName
    LName = LName
    NameLbl.Text = FName + " " + LName
    If Dob Is "" Then
        DOBLbl.Text = "Date of Birth not on file."
    Else
        DOBLbl.Text = Dob
    End If
    PhoneLbl.Text = Phone
    Dim TelephoneModel As String
    If PhoneType Is Nothing Then
        PhoneTypeLbl.Text = "USER IMEI NOT ON FILE, ENTER IMEI INTO DATABASE!"
        TelephoneModel = Nothing
    Else
        PhoneTypeLbl.Text = PhoneType
        TelephoneModel = PhoneType.Substring(0, PhoneType.IndexOf(" "))
    End If
    IMEINumLbl.Text = IMEI


    If TelephoneModel = "Apple" Then
        UsernameLbl.Text = "AppleID Username:"
        UsernameOutputLbl.Text = AppleGoogleID
        PasswordLbl.Text = "AppleID Password:"
        PassLbl.Text = AppleGooglePass
    ElseIf TelephoneModel = "Samsung" Then
        UsernameLbl.Text = "GoogleID Username:"
        UsernameOutputLbl.Text = AppleGoogleID
        PasswordLbl.Text = "GoogleID Password:"
        PassLbl.Text = AppleGooglePass
    ElseIf PhoneType Is Nothing Then
        UsernameLbl.Text = "Username:"
        UsernameOutputLbl.Text = AppleGoogleID
        PasswordLbl.Text = "Password:"
        PassLbl.Text = AppleGooglePass

    End If
    If LockScreen = "" Then
        LockLbl.Text = "None on File"
    Else
        LockLbl.Text = LockScreen
    End If
    PhoneSwapsLbl.Text = Phoneswap
    SwapCountLbl.Text = UserData.SwapCountNumber(FName, LName)

    Dim name = UserData.pictureNameFormatting(FName, LName)
    Dim Folder As String = "G:\PUBLIC\HelpDesk\Public\Data\pictures"
    Dim Filename As String = System.IO.Path.Combine(Folder, name & ".JPG")
    PictureBox1.BackgroundImage = Image.FromFile(Filename)
    PictureBox1.BackgroundImageLayout = ImageLayout.Stretch

End Sub

Private Sub CloseBtn_Click(sender As Object, e As EventArgs) Handles CloseBtn.Click
    Me.Close()
End Sub

Private Sub PrintBtn_Click(sender As Object, e As EventArgs) Handles PrintBtn.Click

End Sub

Private Sub RecQuery_Click(sender As Object, e As EventArgs) Handles RecQuery.Click
    Try
        Dim IMEI As String = IMEINumLbl.Text
        Dim MobileInv As New MobileInventory
        MobileInv.SqlBlob2File(IMEI)
    Catch ex As Exception
        MessageBox.Show("No Receipt on File.")
    End Try

End Sub

Private Sub PhoneHistoryBtn_Click(sender As Object, e As EventArgs) Handles PhoneHistoryBtn.Click
    Dim SwapHistory As New SwapHistory
    SwapHistory.Show()


End Sub
End Class

我需要在下面的表单中填充变量以进行数据操作。

Public Class SwapHistory
Public FName = UserProfile.FName
Public LName = UserProfile.LName

Private Sub SwapHistory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'UserCellPhoneHistory._UserCellPhoneHistory' table. You can move, or remove it, as needed.
    Me.UserCellPhoneHistoryTableAdapter.Fill(Me.UserCellPhoneHistory._UserCellPhoneHistory)
    NameLbl.Text = LName + ", " + FName
End Sub

Private Sub CancelBtn_Click(sender As Object, e As EventArgs) Handles CancelBtn.Click
    Me.Close()
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
End Class

1 个答案:

答案 0 :(得分:3)

最简单的方法之一是自定义New构造函数:

this.ngZone.run(() => {
  //Your promise code
});

然后显示表格:

Public Class SwapHistory
Public FName = UserProfile.FName
Public LName = UserProfile.LName

Public Sub New(fName as String, lName as string)

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Fname = fName
    LName = lName
End Sub
Private Sub SwapHistory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'UserCellPhoneHistory._UserCellPhoneHistory' table. You can move, or remove it, as needed.
    Me.UserCellPhoneHistoryTableAdapter.Fill(Me.UserCellPhoneHistory._UserCellPhoneHistory)
    NameLbl.Text = LName + ", " + FName
End Sub

Private Sub CancelBtn_Click(sender As Object, e As EventArgs) Handles CancelBtn.Click
    Me.Close()
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
End Class