从另一种形式调用私有方法 [英] call private method from another form

查看:70
本文介绍了从另一种形式调用私有方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜。我有一个主要表格。当我点击btnMain时,一个LoginForm将打开。在loginForm中我点击了一个按钮(btnok)。在我从主窗体调用SecureMethod的事件中,做了一些事情并再次显示MainForm(文本框值不会改变)。我希望这个调用(SecureMethod)非常安全,当我转回ro MainForm Textbox值没有改变。提前谢谢

hi. I have a main form. when I click btnMain, a LoginForm will be open.in loginForm I clicked a button(btnok).in the event I call a SecureMethod from main form, doing something and show MainForm again(textbox values don't change). I want this calling(SecureMethod) be very secure and when I turn back ro MainForm Textbox values haven't change.Thanks in advance

public partial class MainForm : Form
{
   private void btnMain_Click(object sender, EventArgs e)
    {
        LoginForm lfrm = new LoginForm;
        LoginForm.ShowDialog();
    }
   private void SecureMethod(){//do sth};
}
public partial class LoginForm : Form
{
   private void btnok_Click(object sender, EventArgs e)
    {
        SecureMethod(); //is not true
        this.close(); //close loginform
    }
}

推荐答案

请记住,表单本质上是类。私有方法不能在类之外调用,这就是信息保密的方式。你需要一个可以访问私有方法的公共方法。



一个简单的例子你可以查看这个很好的教程。

C#私有方法 [ ^ ]
Remember that forms are essentially classes. Private methods can not be called outside of their class, that's how the information is kept private. You will need a public method that can access the private method.

For a simple example you can check this nice tutorial.
C# Private Method[^]


如果 SecureMethod 是私有的,那么您永远不能以其他形式调用它。我认为您希望 SecureMethod 只能在同一个程序集中访问,因此请使用内部 [ ^ 。然后,您需要将 MainForm 传递给 LoginForm

If SecureMethod is private, then you can never call it in another form. I think that you want that SecureMethod is only accessible in the same assembly, so use internal[^]. Then, you'll need to pass the MainForm to the LoginForm:
public partial class MainForm : Form
{
   private void btnMain_Click(object sender, EventArgs e)
    {
        LoginForm lfrm = new LoginForm;
        lfrm.mainFrm = this;
        LoginForm.ShowDialog();
    }
   internal void SecureMethod()
   {
       //do sth
   }
}
public partial class LoginForm : Form
{
   public MainForm mainFrm = null;
   private void btnok_Click(object sender, EventArgs e)
    {
        if (mainFrm != null)
        {
            SecureMethod(); //is not true
            this.Close(); //close loginform
        }
        else
        {
             // mainFrm is null, you can't call SecureMethod
        }
    }
}


安全在登录方案中似乎是您的目标,我将基于 的代码的架构。当然,还有不同的安全性,包括旨在阻止随意黑客攻击的轻量级技术,然后使用.NET中的Windows加密工具,以及集成的非常昂贵的专业第三方工具数据库和网络。



我将用一个WinForms架构草图来说明基于以下原则的轻量级安全登录:



1.在用户成功登录之前,应用程序的主表单不应创建



2.登录过程和主应用程序之间绝对没有耦合(依赖)。登录表格应该没有主表格的知识或意识,主表格不了解或不了解登录表格。



结构:



1.两种表格,登录和MainForm。



a。 LogIn:Buttons btnCancel和btnLogIn,Textbox tbUserName for user name,MaskedTextBox tbMaskedPassword for password entry。



b。 MainForm:一个标签,'lblUserInfo



2. .NET WinForms应用程序设置为单个启动项目,而Program.cs文件像往常一样,设置为启动对象。



Program.cs文件主要方法:
Since security in a log-in scenario appears to be your goal here, I'd base the "architecture" of the code on that. Of course, there's different degrees of security, ranging from lightweight techniques designed to stop "casual" hacking, then on to using Windows cryptography tools in .NET, up to very expensive professional third-party tools that integrate with databases and networks.

I'll illustrate with a sketch of a WinForms architecture for a "light-weight" secure log-in that is based on the following principles:

1. the Application's Main Form should not be created until the user has successfully logged in.

2. there should be absolutely no "coupling" (dependency) between the log-in process and the main Application. The Log-in Form should have no "knowledge or awareness" of the Main Form, and the Main Form have no knowledge or awareness of the Log-in Form.

Structure:

1. Two Forms, LogIn, and MainForm.

a. LogIn: Buttons btnCancel and btnLogIn, Textbox tbUserName for user name, MaskedTextBox tbMaskedPassword for password entry.

b. MainForm: one Label, 'lblUserInfo

2. The .NET WinForms app is set to be a single start-up Project, and the Program.cs file, as usual, set to be the start-up Object.

The Program.cs file Main method:
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new LogIn());
    Application.Run(new MainForm());
}

标准WinForms Program.cs文件的这种变体表现出以下行为:



1.首先是一个LogIn表单创建并显示,应用程序将不会继续,直到LogIn表单被处理(关闭)。



2.在LogIn表单关闭后,MainForm是创建并显示。



3.随时在LogIn表单中调用Application.Exit()将终止应用程序。



LogIn表格:

This variation on the standard WinForms Program.cs file exhibits the following behavior:

1. first a LogIn Form will be created and shown, and the Application will not continue until the LogIn Form is disposed (closed).

2. after the LogIn Form is closed, then the MainForm is created and shown.

3. calling Application.Exit() at any time in the LogIn Form will terminate the Application.

LogIn Form:

public partial class LogIn : Form
{
    public LogIn()
    {
        InitializeComponent();
    }

    // note the use of static variables here !
    private static int nTries = 3;
    private static int tryNumber = 1;

    private void btnLogin_Click(object sender, EventArgs e)
    {
        if (tryNumber > nTries)
        {
            this.Hide();
            MessageBox.Show("Access denied");
            Application.Exit();
        }

        tryNumber++;

        // simulate SecureMethod #1 ...
        if(tbMaskedPassword.Text == "true" && tbUserName.Text == "User")
        {
            // see definition of the LogInData Class
            LogInData.logInOk = true;
            LogInData.uName = tbUserName.Text;
            this.Close();
        }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

LogInData static Class:

public static class LogInData
{
    public static bool logInOk { get; set; }
    public static string uName { get; set; }
}

讨论:



1.在LogIn表单中使用静态变量来计算登录的次数;如果超出限制,将显示登录被拒绝消息,并终止应用程序。



2.如果登录成功,则用户名输入的密码是通过在静态LogInData类中设置静态变量来保存的,此时LogIn Form已关闭。



在MainForm的Load EventHandler中:

private void Form1_Load(object sender, EventArgs e)
{
    if(LogInData.logInOk)
    {
        // call SecureMethod #2 here ...
        // if SecureMethod #2 fails: Appliction.Exit();

        lblUserInfo.Text += LogInData.uName + " logged in: " + DateTime.Now.ToLongDateString();
    }
    else
    {
        Application.Exit();
    }
}

这里有一个额外的......以及真正不必要的进一步验证水平,只是为了提高两级验证的可能性。现在代码的方式,如果登录过程失败,永远不应该加载/显示MainForm。



大图:



1.在这种情况下,静态LogInData类用作存储库,用于成功登录的结果

There's an extra ... and really unnecessary level of further validation sketched in here just for the sake of raising the possibility of two-level validtion. The way the code is now, the MainForm should never be loaded/shown if the log-in process failed.

Big picture:

1. in this case the static LogInData Class is used as a repository for the result of a successful log-in


这篇关于从另一种形式调用私有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆