这是我的 C# ADO.NET 应用程序。我正在尝试将登录用户的名称从我的 LoginForm 传递到我的 MainMenuForm。名称必须在我的 MainMenuForm 标签上可见。登录成功后,我将关闭我的 LoginForm 并显示我的 MainMenuForm。我为此创建了属性,但我在 MainMenuForm 中的标签什么也没显示。这是我的代码:
登录表单代码:
Persons LoggedPerson = new Persons();
public LoginForm()
{
InitializeComponent();
tbPassword.PasswordChar='*';
tbPassword.MaxLength = 9;
}
private void button1_Click(object sender, EventArgs e)
{
Boolean flag = false;
string username= tbUsername.Text;
string password = tbPassword.Text;
var sadrzaj = new NFC_baseEntities1();
foreach (var x in sadrzaj.Persons) {
if (x.Username==username && x.Password==password)
{
LoggedPerson.Name= x.Name;
LoggedPerson.Surname= x.Surname;
flag = true;
break;
}
}
if (flag == false)
{
DialogResult dialog = MessageBox.Show("Invalid username or password!", "Error", MessageBoxButtons.OK);
if(dialog==DialogResult.OK)
Application.Exit();
}
else
{
DialogResult dialog = MessageBox.Show("Username and password are correct!", "Message", MessageBoxButtons.OK);
if (dialog == DialogResult.OK)
{
MainMenu forma = new MainMenu();
forma.Show();
this.Hide();
}
}
}
public string TheName
{
get { return this.LoggedPerson.Name + " " + LoggedPerson.Surname; }
}
MainMenuForm 代码:
public MainMenu()
{
InitializeComponent();
LoginForm nova = new LoginForm();
MenuLbLoggedPerson.Text += nova.TheName;
}