Home > Mobile >  How do I display text from another window in windows form c#?
How do I display text from another window in windows form c#?

Time:03-04

The goal is to display the text from user input using radio button and combo box. I'm using Visual Studio, by the way.

This is the code from the first window:

private void btn_apply_Click(object sender, EventArgs e)
{
    firstName = tbx_firstName.Text;
    middleName = tbx_middleName.Text;
    lastName = tbx_lastName.Text;
    completeName = firstName   " "   middleName   " "   lastName   " ";

    gender = lbl_gender.Text;
    level = lbl_level.Text;
    status = lbl_status.Text;


    if (rbtn_male.Checked)
    {
        lbl_gender.Text = rbtn_male.Text.ToUpper();
    }

    else
    {
        lbl_gender.Text = rbtn_female.Text.ToUpper();
    }

    //Level

    if (rbtn_gradeEleven.Checked)
    {
        lbl_level.Text = rbtn_gradeEleven.Text.ToUpper();
    }

    else if (rbtn_gradeTwelve.Checked)
     {
        lbl_level.Text = rbtn_gradeTwelve.Text.ToUpper();
    }

    else if (rbtn_iST.Checked)
    {
        lbl_level.Text = rbtn_iST.Text.ToUpper();
    }

    else if (rbtn_iSM.Checked)
    {
        lbl_level.Text = rbtn_iSM.Text.ToUpper();
    }

    else
    {

    }

    //Admission Status

    cbx_status.SelectedItem.ToString();


    //Form2

    this.Hide();
    Form2 output = new Form2();
    output.Show();

Meanwhile, this is the code for the second window (Result):

private void Form2_Load(object sender, EventArgs e)
{
    lbl_displayName.Text = Form1.completeName;
    lbl_displayGender.Text = Form1.gender;
    lbl_displayLevel.Text = Form1.level;
    lbl_displayStatus.Text = Form1.status;
}

Thank you!

CodePudding user response:

when Form2 should display elements from Form1, the former surely has to have a reference to the latter. So start giving Form2 a reference to your Form1-instance, e.g. using a constructor-arg on Form2.

However usually you don't want to pass controls or forms around, but just specific values within those. In your case those are the gender, name the level and the status. So just provide those arguments to Form2, not the entire Form1-instance:

class Form2 : Form
{
    private readonly string name; // ...
    public Form2(string name, string status, string level, string gender)
    {
        this.name = name;
        // ...
    }
}

Now you can easily access those variables within your Load-event:

private void Form2_Load(object sender, EventArgs e)
{
    lbl_displayName.Text = this.name;
    lbl_displayGender.Text = this.gender;
    lbl_displayLevel.Text = this.level;
    lbl_displayStatus.Text = this.status;
}

CodePudding user response:

Change:

Form2 output = new Form2();
output.Show();

To:

Form2 output = new Form2();
output.Show(this); // <-- Form1 as the Owner of output

Now in Form2, simply cast .Owner back to Form1:

private void Form2_Load(object sender, EventArgs e)
{
    Form1 f1 = (Form1)this.Owner;
    lbl_displayName.Text = f1.completeName;
    lbl_displayGender.Text = f1.gender;
    lbl_displayLevel.Text = f1.level;
    lbl_displayStatus.Text = f1.status;
}
  • Related