将用户名传递给表单,但用户名返回空 c# [英] passing username to form but username returns null c#
问题描述
此代码用于返回登录表单中文本框的值.
This code is to return the value of textbox in the Login form.
public partial class Login : Form
{
public string returnUsername()
{
string username = textBox1.Text;
return username;
}
}
此代码用于显示 ChangePass 表单.
This code is to allow the ChangePass form to show.
public partial class Mainmenu_Employee : Form
{
private void changePasswd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
this.Hide();
Login login = new Login();
ChangePass passwd = new ChangePass(login);
passwd.Show();
}
}
此代码用于从登录表单中获取用户名,以便我可以更改用户名的密码.
This code is to take the username from Login form so that I can change the password of the username.
public partial class ChangePass : Form
{
Login login = null; //parent form
Mainmenu_Employee main = new Mainmenu_Employee();
public ChangePass(Login login1)
{
InitializeComponent();
login = login1;
}
private void buttonChangePass_Click(object sender, EventArgs e)
{
Model_DB_Employee emp = new Model_DB_Employee();
//Login login = new Login();
string username = login.returnUsername();
if (textBoxNewPass.Text == string.Empty || textBoxConfirmPass.Text == string.Empty)
{
MessageBox.Show("Field cannot be empty!");
}
else
{
if (textBoxNewPass.Text == textBoxConfirmPass.Text)
{
try
{
emp.changePasswd(username,textBoxConfirmPass.Text);
MessageBox.Show(username);
MessageBox.Show("Password updated!");
this.Hide();
main.Show();
}
catch(SystemException ex)
{
MessageBox.Show("Password not updated" + ex);
}
}
else
{
MessageBox.Show("Passwords do not match!");
}
}
}
修改密码功能:
public void changePasswd(string username, string newpass) //change password
{
Model_Employee emp = new Model_Employee();
//Hasher hash = new Hasher(); //call hasher class for hashing
//string hashed;
//string salt = emp.generateSalt(); //generate random salt
//newpass = newpass + salt; //append salt to newpass
//hashed = hash.encryption(newpass); //hash newpass
for (int i = 0; i < rows.Count; ++i)
{
if ((string)empTab.Rows[i]["username"] == username)//check if ID matches
{
empTab.Rows[i]["passwd"] = newpass; //set passwd to hash new password
//check if dataset has changes
if (dataset.HasChanges())
{
//update database
dbAdapter.Update(dataset, "employee");
MessageBox.Show("Employee Updated!");
refreshTable();
}
else
{
refreshTable();
}
}
}
}
我试图在用户登录时更改其密码.
I am trying to change a user's password when he is logged in.
当他登录时,我想通过文本框获取他的用户名.
When he logs in, I want to capture his username through a textbox.
他登录后,会显示一个主菜单.
After he logs in, there will be a main menu displayed.
用户需要点击更改密码链接,系统会显示更改密码表单.
The user needs to click on the change password link and a change password form will appear.
因此,我需要将用户名从登录表单传递到更改密码表单才能使用更改密码功能.但是,我现在面临的问题是用户名没有从登录表单传递到更改密码表单.
Therefore, I need to pass the username from the login form to the change password form in order to use a change password function. However, the issue I am facing now is that the username does not get passed from the login form to the change password form.
推荐答案
问题在于线路:
Login login = new Login();
这是用 Login
的新实例作为局部变量遮蔽 login
实例字段,而不是访问之前创建的 Login
用户已与之交互,您正在访问一个空白的.您可以删除上面的代码行.
This is shadowing the login
instance field with a new instance of Login
as a local variable, so rather than accessing the Login
created earlier that the user has interacted with, you're accessing a blank one. You can just delete the above line of code.
这篇关于将用户名传递给表单,但用户名返回空 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!