类无法获取maindow表单的值

本文关键字:表单 maindow 获取 | 更新日期: 2023-09-27 18:25:31

我是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表单的值

您的问题是在Login方法中创建了一个名为MainWindowPage的新MainWindow,其中Username_txtbox的默认文本为空(我想是这样)。MainWindowPage不是您程序的主窗口。您必须通过Login方法中的当前主窗口。修改你的代码如下(只是一点点修改):

public void Login(MainWindow MainWindowPage)
{
    DatabaseConnect.ConnectDatabase();
    //....
}

而且。。。

private void Done_btn_Click(object sender, RoutedEventArgs e)
{
    LoginControl.Login(this);
}