How can share the value between more Windows Forms ?

  • Thread starter Thread starter Kylin
  • Start date Start date
K

Kylin

hi,
Where the user input there Login Name and Password,
and I want to remerber this values In maninform,
and this values will be used by other form,
eg: Change password Form,
this need the user's name ,oldpassword, new password,
and the user's name comes from the mainform.

how can do for this ?
 
Kylin said:
hi,
Where the user input there Login Name and Password,
and I want to remerber this values In maninform,
and this values will be used by other form,
eg: Change password Form,
this need the user's name ,oldpassword, new password,
and the user's name comes from the mainform.

how can do for this ?

You could use properties, or parameters in the constructor. eg.


-- loginForm.cs --
MainForm frm = new MainForm(txtUser.Text, txtPass.Text);
frm.Show()
-- / --

-- MainForm.cs --
public MainForm(string user, string pass)
{
// Put user & pass into private fields
}
-- / --


Or alternatively, add some properties, so you can do

-- loginForm.cs --
MainForm frm = new MainForm();
frm.User = txtUser.Text;
frm.Pass = txtPass.Text
frm.Show()
-- / --

Though if you're doing it that way, a struct to hold the details might
be better:

-- loginForm.cs --
MainForm frm = new MainForm();
frm.UserDetails = new UserDetails(txtUser.Text, txtPass.Text);
frm.Show()

struct UserDetails
{
public string User, Pass;
public UserDetails (string user, string pass)
{
User = user;
Pass = pass;
}
}
-- / --

HTH
 
Back
Top