如何从表单之间的 TextBox 传递字符串值? [英] How do I pass a string value from a TextBox between Forms?

查看:43
本文介绍了如何从表单之间的 TextBox 传递字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表单:Form1 是我的应用程序,Form2 是登录页面.我想将输入到 Form2 上的用户名文本框 (LoginTbox) 的值传递给 Form1.这是我到目前为止.没有收到错误,但似乎什么也没传递.我试过构造函数,但似乎也无法让它工作.我做错了什么?

I have two forms: Form1 which is my app and Form2 which is a login page. I want to pass a value entered into the username textbox (LoginTbox) on Form2 to Form1. This is what I have so far. No error is received, but it seems to be passing nothing. I've tried constructors, but couldn't seem to get that to work either. What am i doing wrong?

程序.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();
    }

Form2(登录表单)

Form2 (Login Form)

    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(应用)

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");
        }
    }

推荐答案

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

You're dealing with two completely separate instances of Form2. Your first instance, which the user used to login, is inaccessible from within Form1. The instance of Form2 that you created inside the button click event in Form1 only has the initial value of string.Empty stored in strVar.

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

To get it up and working, I'd change your Main method to pass the value you need into the constructor of Form1:

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

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

And then modify the constructor of Form1 (which I don't see in your snippet) to accept that argument:

private string userName = string.Empty;

public Form1(string userName)
{
    InitializeComponent();

    this.userName = userName;
}

去掉Form1中按钮点击事件中Form2的单独实例.

Get rid of the separate instance of Form2 inside the button click event in Form1.

顺便说一下,如果有人弄清楚您当前如何将文本框值传递到数据库,他们可能会输入类似 ';DELETE FROM ACCOUNTS; 进入 textBox2.Text 并造成严重破坏.(我还没有专门尝试过这个,但类似的东西可能会起作用......)

As a side FYI, if someone figures out how you're currently passing your textbox values to the database, they may be able to type something like '; DELETE FROM ACCOUNTS; into textBox2.Text and wreak havoc. (I haven't tried this specifically but something similar may work...)

如果您完全好奇,请查找有关 SQL 注入攻击的文章,例如 这个.

If you're curious at all, look up articles on SQL injection attacks, such as this one.

这篇关于如何从表单之间的 TextBox 传递字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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