0

我有一个登录表单“ frmLog()”,其中包含用户名和密码的文本框,现在我想从用户名文本框中获取输入文本。请参阅下面的代码,不要混淆我使用的是 mysql 数据库,因此其他语法代码并不重要。

这是登录表单“ frmLog()

 private void button1_Click(object sender, EventArgs e)
        {
            ApareceCrudLib a = new ApareceCrudLib("localhost", "root", "", "cashieringdb");
            string user = txtLogin.Text;
            string pass = txtPassword.Text;
            string query = "SELECT * FROM register WHERE username='" + user + "' AND password=MD5('" + pass + "')";
            int result = a.Count(query);
            if (result == 1)
            {
                frmMain main = new frmMain();
                main.ShowDialog();
                this.Dispose();
            }
            else
            {
                MessageBox.Show("Login Failed! Try Again");
                txtLogin.Text = "";
                txtPassword.Text = "";
            }
        }

这是我要检索代码“ frmMain()”的另一种形式。请参阅public void dataLog()这是我尝试从中获取输入值的代码部分frmLog

private void frmMain_Load(object sender, EventArgs e)
{
    a = new ApareceCrudLib("localhost", "root", "", "cashieringdb");
    loadDataGridView_Main();
    dataLog();
}
public void loadDataGridView_Main()
{
    dgvMain.Rows.Clear();
    List<string>[] detailList = a.mysqlSelect("Select * From sales");
    for (int i = 0; i < detailList.Length; i++)
    {
        dgvMain.Rows.Add(detailList[i][0], detailList[i][1], detailList[i][2], detailList[i][3]);
    }
}
public void dataLog()
{
    frmLog kk = new frmLog();
    txtLog.Text= kk.txtLogin.ToString();
}

这是错误的结果

在此处输入图像描述

结果必须是frmLog()示例“client123”中的用户名。你觉得这里有什么问题?

4

3 回答 3

2

kk.txtLogin.ToString(), returns a string that represents the current object. In this case your current object is TextBox.

Instead display the TextBox, you should show the TextBox's value. Change that code to:

kk.txtLogin.Text;

UPDATE

If you want to display the txtLogin value from frmLog in frmMain, you can declare static variable in frmLog that store the txtLogin value.

in frmLog:

public static String LOGIN_USER = "";

Then in button1_Click:

    if (result == 1)
    {
      LOGIN_USER = txtLogin.Text;
      frmMain main = new frmMain();
      main.ShowDialog();
      this.Dispose();
    }

in frmMain_Load:

    public void dataLog()
    {        
       txtLog.Text= frmLog.LOGIN_USER;
    }
于 2013-03-30T15:45:26.403 回答
0

In your datalog() Methode is the first error.

Replace:txtLog.Text= kk.txtLogin.ToString();

With:txtLog.Text= kk.txtLogin.Text;

In:

 public void dataLog()
 {
        frmLog kk = new frmLog();
        txtLog.Text= kk.txtLogin.ToString();
 }

You create a new frmlog object without showing it, so the textbox is empty. You should retrieve the text from the text box after the user input.

于 2013-03-30T15:47:20.557 回答
0

It is so because you are trying to read data from a new instance of your frmLog();

In you frmMain, do not write this :

frmLog kk = new frmLog();

instead create a public reference in your form class

public frmLog kk;

然后,在你的 frmLog 中,

frmMain main = new frmMain();
main.kk=this; 
main.ShowDialog();

这将以第二种形式创建对第一种形式的引用。

于 2013-03-30T16:11:22.147 回答