简体   繁体   English

在表单之间传递值时出错

[英]Error passing values between forms

while passing username and password from form2 to form3 , for the first time when i run the application , the value does not get passed for the first time . 当我将用户名和密码从Form2传递到Form3时,首次运行该应用程序时,该值并未首次传递。 however when i re - run the application , it works everytime and the addition is successful , im really new to c# and windows programming so please forgive any poor executions . 但是,当我重新运行该应用程序时,它每次都能正常运行,并且添加成功,这对于c#和Windows编程来说确实是新的,因此请原谅任何不良的执行。 flow is form1->form2->form3 流是form1-> form2-> form3

UPDATE : this is the definition for form 3 更新:这是表格3的定义

  public partial class Form3 : Form
    {
        MySqlConnection connection;
        public string username;
        public string password;
        public Form3(string user,string pass)
        {
            username = user;
            password = pass;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(username, password);
            string connstring = "server=localhost;user=" + username + ";database=testdb;port=3306;password=" + password + ";";
            connection = new MySqlConnection(connstring);
            connection.Open();
            string query = "INSERT INTO user_books values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "');";
            MySqlCommand newcommand = new MySqlCommand(query,connection);
            try
            {
                newcommand.ExecuteNonQuery();
                MessageBox.Show("DONE");
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }
            connection.Close();

        }

        private void Form3_Load(object sender, EventArgs e)
        {
            MessageBox.Show(username, password);
        }
    } 

and this is form2 : 这是form2:

 public partial class Form2 : Form
    {
        public static MySqlConnection connection;

        public string username;
        public string password;

        public Form2(string user,string pass)
        {
            InitializeComponent();
            this.Text = "MAIN MENU";
            username = user;
            password = pass;
        }

        Form3 form3 = new Form3(username,password);
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(username,password);//this lines  no longer gives an empty message box with title Error
            form3.Show();
        }
    }

this is form 1 : 这是表格1:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = "LOGIN";
        }

        MySqlConnection connection;
        Form2 form2;
        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string password = textBox2.Text;

            string connstring = "server=localhost;user=" + username + ";database=testdb;port=3306;password=" + password + ";";

            try
            {
                connection = new MySqlConnection(connstring);
                connection.Open();

                form2 = new Form2(username,password);
                form2.Show();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString(), "Exception");
            }
            connection.Close();
        }
    }

using mysql and visual studio 2012 使用mysql和visual studio 2012

Please give proper names to your forms and drop all static variables. 请给您的表格起适当的名称,并删除所有静态变量。

Move the creation of Form3 into the event handler, so it will get the username/password that are set at the time of the click, not when the class is constructed. 将Form3的创建移动到事件处理程序中,这样它将获得在单击时(而不是在构造类时)设置的用户名/密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM