将值传递给另一个表单c#

时间:2014-08-07 10:51:10

标签: c# visual-studio-2010

如何将txtFirstName.Text的值传递给另一个表单?以及其他输入。(我粘贴了我正在处理的两个表格)。请帮我。或者使用MultiDimensional数组更好?我正在做类似帐户的登录注册程序,您可以在其中查看您在配置文件中输入的最多5个帐户的信息。请帮忙。三江源。

public partial class frmInfo : Form
{
    public frmInfo()
    {
        InitializeComponent();
    }

    private void btnConfirm_Click(object sender, EventArgs e)
    {
        if (txtFirstName.Text == String.Empty)
        {
            errorProvider1.SetError(txtFirstName, "Your First Name is important.");
        }
        else
        {
            errorProvider1.Clear();
        }

        if (txtLastName.Text == String.Empty)
        {
            errorProvider2.SetError(txtLastName, "Your Last Name is important.");
        }
        else
        {
            errorProvider2.Clear();
        }

        if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty)
        {
            frmProfile profile = new frmProfile();
            profile.Show();
            this.Hide();
        }
    }
}


//Other form
public partial class frmProfile : Form
{
    public frmProfile()
    {
        InitializeComponent();
    }

    private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmChangePassword changepass = new frmChangePassword();
        changepass.Show();
        this.Hide();
    }

    private void logOutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmMain logout = new frmMain();
        logout.Show();
        this.Hide();
    }
}

3 个答案:

答案 0 :(得分:2)

您正在使用面向对象语言,所以为什么不尝试使用类并传递此类的实例。

首先使用相关属性

定义您的类
public class Profile
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    .... other properties will follow as you like...
}

现在在第一个表单上按钮的点击事件

private void btnConfirm_Click(object sender, EventArgs e)
{
    Profile pf = new Profile();
    if (txtFirstName.Text.Trim() == String.Empty)
    {
        errorProvider1.SetError(txtFirstName, "Your First Name is important.");
        return; 
    }
    else
    {
        errorProvider1.Clear();
        pf.FirstName = txtFirstName.Text;
    }
    .......

    // Pass your Profile class instance to the constructor of the frmProfile
    frmProfile profile = new frmProfile(pf);
    profile.Show();
    this.Hide();
}

现在在frmProfile类中使用传递的实例

public partial class frmProfile : Form
{
    public frmProfile(Profile pf)
    {
        InitializeComponent();
        txtFirstName.Text = pf.FirstName;
        .....
    }
    .....
}

通过这种方式,您只需传递一个包含所有数据的变量,而无需将每个文本框传递到frmProfile表单

答案 1 :(得分:0)

你可以做到

frmProfile profile = new frmProfile();
profile.txtFullName.Text = String.Format("{0} {1}",  
                                         txtFirstName.Text, 
                                         txtLastName.Text);
profile.Show();

答案 2 :(得分:0)

您可以将两个参数添加到frmProfile的构造函数中。然后,在构造表单的新实例时,将文本框的值传递给表单。

frmProfile的构造函数:

public frmProfile(string Firstname, string Lastname) {
    // Do something with Firstname and Lastname
    InitializeComponent();
}

然后在btnConfirm_Click函数中:

    if (txtFirstName.Text != String.Empty && txtLastName.Text != String.Empty) {
        frmProfile profile = new frmProfile(txtFirstName.Text, txtLastName.Text);
        profile.Show();
        this.Hide();
    }
相关问题