1

I have created two forms in the designer, formA and formB, one is a default parent form and the latter is a modified "about box". I need to know the best way to get data of a two dimensional array to formB from formA, but so far I've only got it to "work" when formA was inadvertently opened a second time alongside formB.

Basic code at the moment is:

// Form A (onload)
public string[,] arrayname = new string[5, 2] { some values };
// Form A (onevent)
formB f2 = new formB(arrayname);
f2.Show();

// Form B (onload)
???
label1.Text = arrayname[0, 0];
label2.Text = arrayname[0, 1];
label3.Text = arrayname[1, 0];
...

Thanks in advance!

3 Answers 3

2

The easiest way for me is to change the child form (FormB) constructor to accept an array of String as parameter :

private string[,] arrayname;
public FormB(string[,] _arrayname)
{
this.arrayname = _arrayname;
}

and when you create your instance of FormB :

formB f2 = new formB(arrayname);
f2.Show();
Sign up to request clarification or add additional context in comments.

1 Comment

+1 This is probably how I would design it. Additionally, I'd make the default constructor private to communicate arrayName needs values for the form to be useful. private FormB(){}
1

make a public property on FormB

public string [,] SomeArray {get;set;}

Then set it when you display

FormB f = new FormB();
f.SomeArray = this.arrayname;
f.Show();

Then in FormB

this.label1.Text = this.SomeArray[0,0];

Comments

0

Probably the easiest would be to expose it as a property in FormB like so:

public string[,] Hours { get; set; }

And set it right after you create the instance:

FormB myFormB = new FormB();
myFormB.ArrayName = new string[5, 2] { some values };

2 Comments

This seemed to parse an error? 'WindowsFormsApplication1.FormB' does not contain a definition for 'arrayname' and no extension method 'arrayname' accepting a first argument of type 'WindowsFormsApplication1.FormB' could be found (are you missing a using directive or an assembly reference?)
Edited my answer. You need to create a property for FormB first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.