[英]Calling the initial class from inside an object C#
I am creating a Windows Form app in C#, I'm enforcing password protection. 我正在用C#创建Windows Form应用程序,正在执行密码保护。 I'm sorry for the poor title and explanation, as you can tell I'm an amateur.
抱歉,标题和解释不当,您可以说我是业余爱好者。
The form that loads upon login is a class that is called from within Program.cs
登录时加载的表单是在
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());
}
}
}
The class is simply called Login. 该类简称为Login。 Once the user has successfully authenticated, I create a new object of a class, hide the Login form and open a new class.
用户成功通过身份验证后,我将创建一个类的新对象,隐藏“登录”表单并打开一个新类。
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";
}
}
}
}
This works perfectly fine... To open the program and authenticate the user however I wish to be able to logout and open the Login form again from within the RiverbankTeaRooms
class. 这可以正常工作...打开程序并验证用户身份,但是我希望能够从
RiverbankTeaRooms
类中注销并再次打开“登录”表单。 I don't know how I'd be able to re-open the form Login again, as it has only been hidden, but I can't figure out how to do Login.Show();
我不知道如何再次重新打开Login表单,因为它只是被隐藏了,但是我不知道该怎么做
Login.Show();
I can't create a new instance of login inside the main form, because the RiverbankTeaRooms
form won't be able to close. 我无法在主表单中创建新的login实例,因为
RiverbankTeaRooms
表单将无法关闭。
This is driving me crazy, and sorry for the poor explanation! 这让我发疯,对于拙劣的解释深表歉意!
I would like to suggest you to change the flow of the program. 我建议您更改程序流程。
Instead of showing the RiverbankTeaRooms
from the Login
on success, test the login result in your Program.Main
and then show the RiverbankTeaRooms
or any error message. 不要在成功
Login
时显示Login
的RiverbankTeaRooms
,而是在Program.Main
测试登录结果,然后显示RiverbankTeaRooms
或任何错误消息。
Add the following to Login : 将以下内容添加到Login中:
public bool Success { get; private set; }
public static bool Authenticate()
{
var login = new Login();
login.ShowDialog();
return login.Success;
}
And update your Enter_Click : 并更新您的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";
}
}
}
And just use the Authenticate method : 只需使用Authenticate方法:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new RiverbankTeaRooms() { SignedIn = Login.Authenticate() });
}
If you need to authenticate again inside the RiverbankTeaRooms
, you can do it like this : 如果您需要在
RiverbankTeaRooms
内部再次进行身份验证,则可以这样进行:
if (Login.Authenticate())
{
// do stuff...
}
else
{
// show error, or stop the user
}
To answer the titular question, simply pass the Login
instance to the RiverbankTeaRooms
object: 要回答名义问题,只需将
Login
实例传递给RiverbankTeaRooms
对象:
RiverbankTeaRooms main = new RiverbankTeaRooms(this);
//In other form
Login myPrivateLoginReference;
public RiverBanksTeaRoom(Login loginForm)
{
myPrivateLoginReference = loginForm;
}
For your specific case, there are other approaches, such as raising an event from main
that would also work: 对于您的特定情况,还有其他方法,例如从
main
引发事件也可以:
main.ShowLoginRequested += ShowLogin;
void ShowLogin()
{
this.Show();
}
As an aside, please never store passwords as plaintext in a database, as it appears you are doing, you should always be comparing against a hash. 顺便说一句,请不要将密码以纯文本格式存储在数据库中,就像您正在做的那样,您应该始终将其与哈希进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.