我在控制台应用程序中创建了一个类,现在我想创建一个使用该类的windows窗体应用程序

本文关键字:一个 应用程序 创建 windows 窗体 控制台 | 更新日期: 2023-09-27 18:18:54

我创建了一个名为"authenticate"的按钮和两个文本框,以便程序可以获得一些用户输入。当单击身份验证按钮时,我希望程序根据用户名和密码是否匹配返回"已验证"或"未验证"。有人知道怎么做吗?c#新手,谢谢。我添加了下面的类以供参考。

class Authenticator
{
    static void Main(string[] args)
    {
        Authenticator au = new Authenticator();
        au.InitialValues();
       if (au.Authenticate())
          Console.WriteLine("Authenticated");
       else
          Console.WriteLine("NOT Authenticated");
          Console.WriteLine("Press Enter to end");
          Console.ReadLine();
     }
     private Dictionary<string, string> dictionary = new Dictionary<string, string>();
     public void InitialValues()
     {
          dictionary.Add("username1", "password1");
          dictionary.Add("username2", "password2");
          dictionary.Add("username3", "password3");
          dictionary.Add("username4", "password4");
          dictionary.Add("username5", "password5");
     }
     public bool Authenticate()
     {
          bool authenticated = false;
          Console.WriteLine("Please enter a username");
          string inputUsername = Console.ReadLine();
          Console.WriteLine("Please enter your password");
          string inputPassword = Console.ReadLine();
          if (dictionary.ContainsKey(inputUsername) && dictionary[inputUsername] == inputPassword)
          {
              authenticated = true;
          }
          else
          {
              authenticated = false;
          }
          return authenticated;
    }
}

我在控制台应用程序中创建了一个类,现在我想创建一个使用该类的windows窗体应用程序

您可以在windows窗体项目(exe-File)中引用Console项目。这不是一个好的做法。最好把你重用的代码放在一个库项目中,它将被控制台和窗体项目引用。

哦,请注意,如果没有控制台,您的代码将无法在Forms项目中工作。

添加类库项目:

在您的解决方案中创建一个新项目(右键单击Add-> new project)并选择Class Library。在此之后,右键单击控制台或表单项目并选择Add->Reference。在对话框中转到Project Reference并检查新创建的Class Library。现在将Main函数以外的类复制到类库项目中并进行编译。只要注意重命名项目即可。

要使身份验证工作,您应该使用参数而不是Console.ReadLine.

在类库

public class Authentication
{
    private Dictionary<string, string> dictionary = new Dictionary<string, string>();
    public Authentication()
    {
        dictionary.Add("username1", "password1");
        dictionary.Add("username2", "password2");
        dictionary.Add("username3", "password3");
        dictionary.Add("username4", "password4");
        dictionary.Add("username5", "password5");
    }
    public bool Authenticate(string user, string password)
    {
        // note i just replaced the variable with return
        return dictionary.ContainsKey(user) && dictionary[user] == password;
    }
}

在控制台应用程序中

Main(...)
{
    // ...
    Console.WriteLine("Please enter a username");
    var user = Console.ReadLine();
    Console.WriteLine("Please enter your password");
    var password = Console.ReadLine();
    var auth = new Authentication();
    if(auth.Authenticate(user, password))
    { // do what you need to do ;) }
}

请注意,您的身份验证机制不安全。因此,不要在非常需要安全性的地方使用它。

嗯,你可以用三个不同的项目来解决这个问题:

  1. Logic(类库项目):它将有一个类与您的验证逻辑方法;
  2. 控制台应用程序:它将有一个对逻辑的引用,并使用其方法来验证和显示控制台的结果;
  3. WinForms应用程序:与控制台相同,但以窗体
  4. 显示结果

我建议:<标题> 逻辑

public class MyLogic()
{
    public void InitializeData()
    {
       ...
    }

    public boolean Authenticate(string userName, string password)
    {
       if(....)
       {
          return true;
       }
       else
       {
          return false;
       }
    }
}
<标题>
public static void main(string[] args)
{
  Console.WriteLine('Enter your username: ');
  string username = Console.ReadLine();
  Console.WriteLine('Enter your password: ');
  string password= Console.ReadLine();
  var logic = new MyLogic();
  logic.InitializeData();
  if(logic.Authenticate(username, password))
  {
    Console.WriteLine("Success")
  }
  else
  {
    Console.WriteLine("Fail");
  }
}
<标题> WinForms应用h1> 这里,你可以做同样的控制台,但在弹出窗口显示结果,在标签或任何你想要的地方。我想你能理解我的意思。

相关文章: