Click here to Skip to main content
15,903,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear guys,

I have 2 Classes called Form1 & Form2. Form1 has variable called password. I want to access the password variable of Form1 by using Form2. I am using vb 2010 express. Can any one help me on this? I have declared password variable as public in Form1.

Thanks.

What I have tried:

Public Class Form1
Public password As String
Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
Close()
End Sub

Private Sub cmdlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdlogin.Click
password = 123
If txtusername.Text = "admin" And txtpassword.Text = password Then
Form2.Show()
Me.Hide()
Else
MsgBox("Login Failed", MsgBoxStyle.OkOnly, "Error")
txtusername.Focus()
End If
End Sub

Private Sub cmdchangepassword_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdchangepassword.Click
Form2.Show()
Me.Hide()
End Sub
End Class

=======================================================================================
Public Class Form2
Public newpassword As String
Private Sub cmdcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdcancel.Click
End
End Sub

Private Sub cmdok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdok.Click
If txtnewpw.Text = txtconfirmpw.Text Then
MsgBox("Password Successfully Changed", vbOKOnly, "Password Changed")
newpassword = txtnewpw.Text
password = newpassword
Else
MsgBox("Password Mismatch", vbOKOnly, "Password Changed")
End If
End Sub

End Class
Posted
Updated 4-Dec-20 21:38pm

1 solution

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]

The code is in C#, but it's pretty obvious, and an online translator like Code Converter C# to VB and VB to C# – Telerik[^]
Transferring information between two forms, Part 3: Child to Child[^] will help with anything you don't understand.

And you do realize that variables are not preserved from run to run of your app? So unless you save your new password somewhere - a file, a DB, whatever - the changes they make will not be applied next time they run your app?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900