0

I want to set values in array from textBoxes in Form1 and send it as Global variable to Form2, but the array does not pass through forms.

class GlobalVariables
{
    private string[] array = new string[3];
    public string[] Array
    {
        get { return array; }
        set { array = value; }
    }
}

If i click button1_Click in Form1, values from textBoxes should be set in array and sent to constructor in class GlobalVariables.

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

    private void button1_Click(object sender, EventArgs e)
    {
        GlobalVariables G = new GlobalVariables();           

        string[] array = new string[3];
        array[0] = textBox1.Text;
        array[1] = textBox2.Text;
        array[2] = textBox3.Text;

        G.Array = array;

        Form2 f2 = new Form2();
        f2.Show();
    }
}

If i click button1_Click in Form2, values from constructor Array should be sent to array and then to labels.

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

    private void button1_Click(object sender, EventArgs e)
    {
        GlobalVariables G = new GlobalVariables();
        string[] array= G.Array;
        label1.Text = array[0];
        label2.Text = array[1];
        label3.Text = array[2];
    }        
}
  • 1
    have you ever heard about static variables? ... anyway ... it is not a good idea ... you will find better solution if you would try to search ... there are plenty similar questions here how to pass variables between forms – Selvin Apr 10 '19 at 11:38
  • @Michael J. Adler: What do you think this line will do: " GlobalVariables G = new GlobalVariables();" in Form2? – popsiporkkanaa Apr 10 '19 at 11:40
  • You can pass anything you like, but for example, you cant set stuff on form2 unless you made it, so before you do f2.show, you could set the values to f2, but you should also provide some method of getting them back.. such as show f2 as modal, and then immediately update those variables back on form1.. is this right? potentially not.. but everyone starts somewhere.. however, there are countless examples out there to find – BugFinder Apr 10 '19 at 11:54
  • [Here](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) is another one – GuidoG Apr 10 '19 at 11:57

1 Answers1

0

You can add a constructor to form2 so that it can receive the array from form1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        GlobalVariables G = new GlobalVariables();

        string[] array = new string[3];
        array[0] = textBox1.Text;
        array[1] = textBox2.Text;
        array[2] = textBox3.Text;

        G.Array = array;

        Form2 f2 = new Form2(array);
        f2.Show();
    }        
}

And for form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    string[] _MyArray = new string[3];
    public Form2(string[] myArray)
    {
        InitializeComponent();
        _MyArray = myArray;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = _MyArray[0];
        label2.Text = _MyArray[1];
        label3.Text = _MyArray[2];
    }       
}
Tendai Mare
  • 31
  • 1
  • 2
  • 14