从对象 C# 内部调用初始类

本文关键字:调用 内部 对象 | 更新日期: 2023-09-27 18:36:22

我正在用 C# 创建一个 Windows 窗体应用程序,我正在强制执行密码保护。我很抱歉标题和解释很糟糕,正如你可以看出我是一个业余爱好者。

登录时加载的表单是从 Program.cs 中调用的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace POS
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }
    }
}

该类简称为登录。用户成功进行身份验证后,我将创建一个类的新对象,隐藏登录表单并打开一个新类。

namespace POS
{
    public partial class Login : Form
    {
        RiverbankTeaRooms main = new RiverbankTeaRooms();
        private void Enter_Click(object sender, EventArgs e)
        {
            bool found = false;
            try
            {
                // Search through database through dr[1] (Table 2) for matching string.
                foreach (DataRow dr in dra)
                {
                    if (dr[1].ToString() == Code.Text)
                    {
                        found = true;
                        // Open main form.
                        main.SignedIn = true;
                        main.Show();
                        this.Hide();
                        break;
                     }
                }
                if (found == false)
                {
                    // Incorrect password.
                    Code.Text = "Incor";
                }
            }
            catch
            {
                // Error, most likely with Database.
                Code.Text = "Error";
            }
        }
    }
}

这工作得很好...但是,要打开程序并对用户进行身份验证,我希望能够注销并从RiverbankTeaRooms类中再次打开登录表单。我不知道如何再次重新打开登录表单,因为它只是被隐藏了,但我无法弄清楚如何Login.Show();我无法在主窗体内创建新的登录实例,因为RiverbankTeaRooms窗体将无法关闭。

这让我发疯,对不起,解释不佳!

从对象 C# 内部调用初始类

我想建议你改变程序的流程。

不要在成功时显示Login中的RiverbankTeaRooms,而是在Program.Main中测试登录结果,然后显示RiverbankTeaRooms或任何错误消息。

将以下内容添加到登录 :

    public bool Success { get; private set; }
    public static bool Authenticate()
    {
        var login = new Login();
        login.ShowDialog();
        return login.Success;
    }

并更新您的Enter_Click:

    private void Enter_Click(object sender, EventArgs e)
    {
        try
        {
            // Search through database through dr[1] (Table 2) for matching string.
            foreach (DataRow dr in dra)
            {
                if (dr[1].ToString() == Code.Text)
                {
                    Success = true;
                    break;
                }
            }
            if (!Success)
            {
                // Incorrect password.
                Code.Text = "Incor";
            }
            else
            {
                this.Close();
            }
        }
        catch
        {
            // Error, most likely with Database.
            Code.Text = "Error";
        }
    }
}

只需使用身份验证方法:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new RiverbankTeaRooms() { SignedIn = Login.Authenticate() });
}

如果您需要在RiverbankTeaRooms内再次进行身份验证,则可以这样做:

if (Login.Authenticate())
{
    // do stuff...
}
else
{
    // show error, or stop the user
}

要回答名义上的问题,只需将Login实例传递给RiverbankTeaRooms对象:

RiverbankTeaRooms main = new RiverbankTeaRooms(this);
//In other form
Login myPrivateLoginReference;
public RiverBanksTeaRoom(Login loginForm)
{
   myPrivateLoginReference = loginForm;
}

对于您的具体情况,还有其他方法,例如从main引发事件,这些方法也有效:

main.ShowLoginRequested += ShowLogin;
void ShowLogin()
{
   this.Show();
}

顺便说一句,请不要密码作为明文存储在数据库中,因为您似乎正在这样做,您应该始终与哈希进行比较。