简体   繁体   中英

Passing values to another form c#

How can i pass the value of the txtFirstName.Text to the other form? and also the other inputs.(I pasted the two Forms I'm working on). Please Help me. Or is it better to use a MultiDimensional array? Im doing a something-like account login-signup program where you can view the information you input in a Profile with max of 5 accounts. Please help. Thankyou.

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

You are working with an Object Oriented Language, so why don't you try to use classes and pass instances of this class.

First define your class with the relevant properties

public class Profile
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    .... other properties will follow as you like...
}

Now in the click event of your button on the first form

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

And now in the frmProfile class use the instance passed

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

In this way you could pass just one variable that contains all of your data without the need to pass every individual textbox to the frmProfile form

You can do

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

You could add two parameters to the constructor of frmProfile. Then, when constructing a new instance of the form, pass the values of your textboxes to the form.

Constructor of frmProfile:

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

Then when in the btnConfirm_Click function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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