Edgar Edgar - 1 month ago 17
Vb.net Question

How Do i pass a Public variable from one form to another

I Am trying to pass 2 variables from one form and have another form open and i want to use thos same variables from the last form to query a sql database to populate it with data based on information from the last form

I need FName and LName from this form Set by parameters passed by the method 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


And i need to populate to variables on this form below for data manipulation.

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

Answer

One of the simplest ways is to customize the New constructor:

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

Then to show the form:

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


End Sub
Comments