我是 C# 语言的新手,我有一个名为Mainwindow
:
public partial class MainWindow : Window
{
ConnectionManager DatabaseConnect = new ConnectionManager();
LoginController LoginControl = new LoginController();
public MainWindow()
{
InitializeComponent();
}
private void Done_btn_Click(object sender, RoutedEventArgs e)
{
LoginControl.Login();
}
private void Close_btn_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DatabaseConnect.ConnectDatabase();
Username_txtbox.Focus();
}
private void Username_txtbox_KeyDown(object sender, KeyEventArgs e)
{
LoginControl.KeyEnter(e);
}
private void Password_txtbox_KeyDown(object sender, KeyEventArgs e)
{
LoginControl.KeyEnter(e);
}
private void Password_txtbox_KeyDown_1(object sender, KeyEventArgs e)
{
LoginControl.KeyEnter(e);
}
}
LoginController 类
class LoginController
{
private ConnectionManager DatabaseConnect = new ConnectionManager();
public void KeyEnter(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
Login();
}
}
public void Login()
{
MainWindow MainWindowPage = new MainWindow();
DatabaseConnect.ConnectDatabase();
System.Text.RegularExpressions.Regex EmailOnly = new Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
//Validates the textbox if it does not match the Regular expression of email address.
if (!EmailOnly.IsMatch(MainWindowPage.Username_txtbox.Text))
{
MessageBox.Show("Invalid email address!");
return;
}
else
{
if (MainWindowPage.Username_txtbox.Text.Length == 0 || MainWindowPage.Password_txtbox.Password.Length == 0)
{
MessageBox.Show("Enter on the login fields.");
}
if (MainWindowPage.Username_txtbox.Text != "" && MainWindowPage.Password_txtbox.Password != "")
{
DatabaseConnect.SqlStringQuery = "SELECT * FROM login_tbl WHERE user_email = '" + MainWindowPage.Username_txtbox.Text + "' AND password = '" + MainWindowPage.Password_txtbox.Password + "'";
DatabaseConnect.Getdata();
if (DatabaseConnect.SQLDR.HasRows == true)
{
DatabaseConnect.SQLDR.Close();
AdminForm AdminForm = new AdminForm();
AdminForm.Show();
MainWindowPage.Close();
}
else
{
DatabaseConnect.SQLDR.Close();
MessageBox.Show("Invalid user account!");
}
}
}
}
}
我尝试将 Login() 代码放在它正确验证的主窗口中,然后我将 Login() 放在 LoginController 类中,但似乎该类无法在主窗口中检索文本框的值,它总是显示“无效的电子邮件地址”,即使我在 maindow 文本框中输入了必要的值
任何的想法?