Link to home
Start Free TrialLog in
Avatar of jschmuff
jschmuffFlag for United States of America

asked on

Passing variable to form2

I am making an example program to refer to for passing variables to a different form in this case form2. I want to pass name from class Names to form2 but I want to have the button in form1 do the passing to. To make that more clear on form1 I have button1 (Save) and button2 (Display). button1 should save the variable to class Names variable name. and from there set label2 on form2 to what is in textbox1 on form1.

Here is my code for program.cs:


public class Names
    {
        private string name;
 
        public string Name
        {
            get
            {
                return (this.name);
            }
            set
            {
                this.name = value;
            }
        }
    }
 
Here is my code for form1.cs:
 
    public partial class Form1 : Form
    {
        Names names = new Names();
        Form2 form2 = new Form2();
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            names.Name = textBox1.Text;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            form2.Show();
        }
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nsanga
nsanga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jschmuff

ASKER

sorry but that doesnt work I have tried that before I came here
You can add another constructot to Form2 that will accept name as a parameter

public Form2(string name)
{
    // now do whatever you want with name;
}

and to pass name to Form2,use this code

Form2 frm = new Form2("some string");

Goran
>> You can add another constructot

its a contructor. Here is how you would do it

private void button2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(names.Name);
}

I am not sure why are you creating instance of Form2 outside the button2_Click void, but if you have some good reason to to so, then here are two more alternatives:

1) set label2 modifier property to internal, so you can access it from form1
2) if you need to set and get label2 text, then create a string property on Form2, and access this property from form1.

// on form 2
string _labelText;

public property labelText
{
    get {return _labelText;}
    set
    {
        _labelText = value;
        label2.text = _labelText;
    }
}

// in form1
private void button2_Click(object sender, EventArgs e)
{
    form2.labelText = names.Name;
    form2.Show();
}


if you only want to set label2 text, then you can use a public method

// in form2
public void SetLabelText(string labelText)
{
    label2.Text = labelText;
}

// in form1
private void button2_Click(object sender, EventArgs e)
{
    form2.SetLabelText(names.Name);
    form2.Show();
}

Goran
Sorry about that I didn't realize I had to set its modifier to internal not private