如何从Forms之间的TextBox传递字符串值?

时间:2013-07-16 03:40:52

标签: c# winforms parameter-passing scope

我有两种形式:Form1是我的应用程序,Form2是登录页面。我想将输入到Form2上的用户名文本框(LoginTbox)的值传递给Form1。这就是我到目前为止所拥有的。没有收到任何错误,但似乎没有任何错误。我已经尝试过构造函数,但似乎无法让它工作。我做错了什么?

Program.cs的

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Form2 fLogin = new Form2();

        if (fLogin.ShowDialog() == DialogResult.OK)
            Application.Run(new Form1());
        else
            Application.Exit();
    }

表格2(登录表格)

    public string strVar = string.Empty;

    public Form2()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        strVar = loginTbox.Text.ToString();           
        string _pass = textBox2.Text;

        string conStr = "Data Source=CA-INVDEV\\RISEDB01;Initial Catalog=RISEDB01;Integrated Security=True";
        string sqlcmd = "select * from accounts where Username=@Username and Password=@Password";
        using (SqlConnection conn = new SqlConnection(conStr))
        {
            conn.Open();
            SqlCommand cmd = new SqlCommand(sqlcmd, conn);
            cmd.Parameters.AddWithValue("@Username", _username);
            cmd.Parameters.AddWithValue("@Password", _pass);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                MessageBox.Show("Login Successful");                                            
            }
            else
            {
                MessageBox.Show("Login Failed Invalid Credentials. Please try again");
                Application.Restart();                  
            }
        }
    }

Form1(App)

private void button7_Click(object sender, EventArgs e)
    {
        if (textBox6.Text != "")
        {
            Form2 frm = new Form2();
            string strValue = frm.strVar;

            string Owner = textBox6.Text;
            string Time = DateTime.Now.ToString(@"MM\/dd\/yyyy h\:mm tt");
            string Serial = textBox4.Text;
            string conStr = "Data Source=CA-INVDEV\\RISEDB01;Initial Catalog=RISEDB01;Integrated Security=True";               
            string sqlcmd2 = "Select * from Sheet1 where Serial#=@Serial#";
            string sqlcmd = "UPDATE Sheet1 SET Owner=@Owner, Checked_In=NULL, Checked_Out=@Checked_Out, Modified_By=@Modified_By WHERE Serial#=@Serial#";
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sqlcmd, conn);
                SqlCommand cmd2 = new SqlCommand(sqlcmd2, conn);
                cmd2.Parameters.AddWithValue("@Serial#", Serial);
                cmd.Parameters.AddWithValue("@Serial#", Serial);
                cmd.Parameters.AddWithValue("@Owner", Owner);
                cmd.Parameters.AddWithValue("@Checked_Out", Time);
                cmd.Parameters.AddWithValue("@Modified_By", strValue);
                SqlDataReader dr = cmd2.ExecuteReader();
                if (dr.HasRows)
                {                       
                    dr.Close();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    Form1_Load();
                }
                else
                {
                    dr.Close();
                    MessageBox.Show("Serial Does Not Exist");
                    textBox4.Clear();
                }
            }
        }
        else
        {
            MessageBox.Show("Owner was not assigned to asset. Please provide a Owner for this asset");
        }
    }

1 个答案:

答案 0 :(得分:1)

您正在处理两个完全独立的Form2实例。您的第一个实例(用户用于登录)无法在Form1内访问。您在Form2中的按钮点击事件中创建的Form1实例的初始值string.Empty仅存储在strVar中。

为了使其正常工作,我将更改您的Main方法,将您需要的值传递给Form1的构造函数:

...
if (fLogin.ShowDialog() == DialogResult.OK)
{
    Application.Run(new Form1(fLogin.strVar));
}
...

然后修改Form1 的构造函数(我在你的代码片段中没有看到)以接受该参数:

private string userName = string.Empty;

public Form1(string userName)
{
    InitializeComponent();

    this.userName = userName;
}

Form2中的按钮点击事件中删除Form1的单独实例。


作为FYI的一方,如果有人知道您当前如何将文本框值传递到数据库,他们可能会在'; DELETE FROM ACCOUNTS;中键入类似textBox2.Text的内容并造成严重破坏。 (我没有特别尝试过,但类似的东西可能会起作用......)

如果您对此感到好奇,请查看有关SQL注入攻击的文章,例如this one