如何将Form1.cs的值转换为另一个.cs文件

时间:2015-03-16 10:22:17

标签: c#

这是Form1.cs中的代码。我想在下一个dbfun.cs中获取f_name,L_name等的值。

private void submit_Click(object sender, EventArgs e)
{
    string f_name = first_name.Text;
    string L_name = last_name.Text;
    string user_email = email.Text;
    string pass = password.Text;
    string depart = department.SelectedItem.ToString();
    string gender = "";
    if (male.Checked)
    {
        gender = "Male";
    }
    else if (female.Checked)
    {
        gender = "Female";
    }
    String agree = accept.Text;
}

2 个答案:

答案 0 :(得分:0)

无事可做。只需在Form2上创建一个对象实例,并使用所需数量的参数将值传递给Form2

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(textBox1.Text);            
        f2.Show();
    }


    public Form2(string value)
    {
        InitializeComponent();
        textBox2.Text = value;
    }

答案 1 :(得分:0)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace ValuesPassingFromformtoform
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }


            private void button1_Click(object sender, EventArgs e)
            {
                string f_name = first_name.Text;
                string L_name = last_name.Text;
                string user_email = email.Text;
                string pass = password.Text;
                string depart = department.SelectedItem.ToString();
                string gender = "";


                Form2 f2 = new Form2(f_name,L_name,user_email,pass,depart);            
                f2.Show();
            }
        }
    }

and Form2.cs is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ValuesPassingFromformtoform
{
    public partial class Form2 : Form
    {
        public Form2(string f_name, string L_name, string user_email, string pass, string depart)
        {
            InitializeComponent();
            textBox2.Text = f_name;

        }

    }
}