【问题标题】:c# getting a string from textbox using get set , than sending it another class?c# 使用 get set 从文本框中获取字符串,而不是发送另一个类?
【发布时间】:2014-05-28 15:57:10
【问题描述】:

我有一个作业,我应该使用用户登录来制作一个会计应用程序。我的问题是,我必须使用 {get,set} 但我不知道如何正确使用它们。有一个名为“UserLogin”的类.cs' 和一种形式 'UserLoginForm.cs'。在 userLoginForm 中有两个 maskedTextBoxes : maskedTextBoxID 和 maskedTextBoxPass。

当我点击 userLoginForm 上的登录按钮时,它会从 userloginclass 中调用一个函数来检查 id 和 pass 是否正确?

问题是当我单击登录按钮时,来自 maskedTextBoxes 的 id 和 pass 不是我在单击登录之前输入的,它们始终是 maskedTextBoxes 的第一个值:空...

如果我输入 123 作为 id 并输入 123 作为 pass 那么如果我在函数中检查它们是否 id 为“123”并且 pass 为“123”,则会出现错误的 id&pass 消息。

这一次:如果我输入 123 作为 id 和 123 作为 pass ,那么如果在函数中检查它们是否 id 是 " " 并且 pass 是 " " (这是他们的第一个值)登录成功。

问题出在哪里?

在用户登录表单中

    public partial class UserLoginForm : Form
{
    public UserLoginForm()
    {
        InitializeComponent();
    }
    public string userID
    {
        get
        {
            return maskedTextBoxID.Text;
        }
    }

    public string userPASS
    {
        get
        {
            return maskedTextBoxPass.Text;
        }
    }


    private void button_enter_Click(object sender, EventArgs e)
    {
        UserLogin ulogcs = new UserLogin();
        ulogcs.checklogin();

在用户登录类中:

    public void checklogin()
    {
        UserLoginForm uform = new UserLoginForm();
        if (uform.userID == "123" && uform.userPASS == "123")
        {
            MessageBox.Show("Welcome to the bank");
            AccountForm aform = new AccountForm();
            aform.Show();
        }
        else
        {
            MessageBox.Show("Error");
        }
    }

对不起语法(我不是母语)

【问题讨论】:

  • 创建登录类的实例并将信息传递给它。或者使 checklogin 方法(它应该接受两个字符串参数)静态并直接调用它。
  • checklogin(), UserLoginForm uform = new UserLoginForm(); 中创建一个新表单。我不认为那是你想要的。
  • 我向 checklogin() 函数发送字符串,正如您和其他 cmets 所说,我的问题已解决。感谢您提供有关创建新表单的帮助和额外信息。

标签: c# class textbox get set


【解决方案1】:

你很亲密。因为您在“UserLogin”类中实例化了一个新的UserLoginForm 对象,所以它以默认数据(空字符串)而不是用户输入的数据开始。

最快的解决方法是通过表单:

private void button_enter_Click(object sender, EventArgs e)
{
    UserLogin ulogcs = new UserLogin(this);
    ulogcs.checklogin();

在用户登录类中:

private UserLoginForm uform;

public UserLogin(UserLoginForm loginForm)
{
   uform = loginForm;
}

public void checklogin()
{       
    if (uform.userID == "123" && uform.userPASS == "123")
    ...
}

在这段代码中,我们使用this 关键字来获取对当前对象(表单本身)的引用,并将其传递给UserLogin 类的参数化构造函数。当我们调用checklogin 时,它会将其存储起来以获取数据。

最好只传递数据:

private void button_enter_Click(object sender, EventArgs e)
{
    UserLogin ulogcs = new UserLogin();
    ulogcs.checklogin(userID, userPASS);

在用户登录类中:

public void checklogin(string user, string password)
{       
    if (user == "123" && password == "123")
    ...
}

在这段代码中,我们认识到userlogin 不应该关心数据来自哪里,它只需要数据。因此,我们更改checklogin 的签名以接受用户名和密码并直接使用它们。不需要对表单的引用(或知道它甚至存在)。

【讨论】:

  • 我向 checklogin() 函数发送字符串,正如您和其他 cmets 所说,我的问题已解决。谢谢。
【解决方案2】:

你需要改变这个函数的定义

 public void checklogin()
    {
        UserLoginForm uform = new UserLoginForm();
        if (uform.userID == "123" && uform.userPASS == "123")
        {
            MessageBox.Show("Welcome to the bank");
            AccountForm aform = new AccountForm();
            aform.Show();
        }
        else
        {
            MessageBox.Show("Error");
        }
    }

 public void checklogin(string user, string pass)
    {
        if (user== "123" && pass == "123")
        {
            MessageBox.Show("Welcome to the bank");
            AccountForm aform = new AccountForm();
            aform.Show();
        }
        else
        {
            MessageBox.Show("Error");
        }
    }

然后调用它

 ulogcs.checklogin(uform.userID,uform.userPASS);

你不需要创建这个

UserLoginForm uform = new UserLoginForm();

因为这将创建新表单,并且不会包含您输入的值

【讨论】:

  • 我向 checklogin() 函数发送字符串,正如您和其他 cmets 所说,我的问题已解决。感谢您提供有关创建新表单的帮助和额外信息。
猜你喜欢
  • 2019-07-07
  • 2012-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多