访问表单2中的公共变量

时间:2014-09-15 05:52:10

标签: c# windows-forms-designer

我正在尝试开发一个Windows窗体应用程序。在这里,我想调用表单1和表单2的变量。我得到了计算机变量的变量。

namespace ComData
{
    public partial class addnew : Form
    {
        string ConnString = "Server=localhost;Database=machinedetails;UID=root;Encrypt=true;";
        public int ComputerId { get; set; } 

        public addnew()
        {
            InitializeComponent();
        }

        private void btnnext_Click(object sender, EventArgs e)
        {
            using (MySqlConnection conn = new MySqlConnection(ConnString))
            {
                using (MySqlCommand comm = new MySqlCommand())
                {
                    if (this.txtbranch.Text != "" && this.txtcostcenter.Text != "")
                    {
                        try
                        {
                            MySqlParameter branchparam = new MySqlParameter("@branch", MySqlDbType.VarChar, 16);
                            MySqlParameter costcenterparam = new MySqlParameter("@costcenter", MySqlDbType.VarChar, 16);

                            comm.Connection = conn;
                            conn.Open();
                            comm.CommandText = "INSERT INTO computerdetails(branch,costcenter) VALUES (@branch, @costcenter);Select last_insert_id();";
                            comm.Parameters.Add(branchparam);
                            comm.Parameters.Add(costcenterparam);
                            comm.Prepare();

                            String branch = txtbranch.Text;
                            String costcenter = txtcostcenter.Text;

                            comm.Parameters[0].Value = branch;
                            comm.Parameters[1].Value = costcenter;

                            MySqlDataReader reader = comm.ExecuteReader();
                            if (reader.HasRows)
                            {
                                reader.Read();
                                ComputerId = Convert.ToInt32(reader[0]);
                                MessageBox.Show("value is" + ComputerId);
                            }



                            this.Hide();
                            newdetails nd = new newdetails();
                            nd.ShowDialog();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Please fill the values");
                    }
                }
            }
        }
    }
}

我想知道如何在表单2中调用此ComputerId变量。请帮助我..谢谢..

4 个答案:

答案 0 :(得分:0)

此代码块属于您打开addnew-form

的form2
addnew testObj = new addnew(); //init
testObj.show() //or testObj.showDialog();
int id = testObj.ComputerId; //getting the id

答案 1 :(得分:0)

您可以将参数化构造函数添加到newdetails表单。然后在那里传递整数值。像这样:

// In newdetails form

private int computerId;

public newdetails(int compId){
computerId = compId;
}

// in addnew form
newdetails nd = new newdetails(ComputerId);

答案 2 :(得分:0)

以新的详细信息形式:

private addNew _addNew { get; set; }

public newdetails(addNEw parent)
{
    InitializeComponent();
    _addNew = parent; 
}

//you can access any public variable at addNew form with:
int test = _addNew.PublicVariableName

添加新表单:

newetails x = new newdetails(this);
x.Show();

答案 3 :(得分:0)

我建议你将Program.cs文件中的ComputerId变量移动到“Program Class”,如下所示:

public class Program
{
     public static int ComputerId;

    //Main and other methods......
}

现在您可以从以下所有表单中访问此变量:

int cid=Program.ComputerId;

尽可能简单。