6
Answers

Problem with passing an array from one form to another using a class

drew

drew

15y
4.6k
1
Kirtan helped me to pass an array from one form to another using a class.  I'm reading the array values into check boxes so they stay checked when a user goes back and forth between the forms.  However when using the class it runs the CheckChanged event everytime the second form is loaded.  (the array values are loaded on the form_Load method) so the message box I show when the user clicks a check box shows every time the form is loaded.

Can I pull the array values out of the form_Load method  or is this the only way to pass them using a class?
here is roughly what I have:
*******      CLASS1        ********

class Class1
    {
        public static string[] tempArray = new string[14];
    }

*******FORM1 (JetBulls)********

private void JetBulls_Load(object sender, EventArgs e)
        {
            string[] form1array = new string[14];
           
            Class1.tempArray = form1array;
        }

*******FORM2 (Seats)********


public void Seats_Load(object sender, EventArgs e)
        {
            form2array = Class1.tempArray;
        }

private void chkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //if the button is checked, seated person shows
            if (chkBox1.Checked)
            {
                Image seated = Image.FromFile("seatedPerson.jpg");
                chkBox1.Image = seated;
                //add seat assignment to array
                form2array[0] = "seated";
            }
            //if the button is not checked, the seat number shows
            else
            {
                Image seat01 = Image.FromFile("seatNum01.jpg");
                chkBox1.Image = seat01;
                form2array[0] = "";
            }
        
MessageBox.Show("Seat 1 \n" + tempName2 + "\n" + " Cost: $200", "Seat Booked");
        }

***





Btw, Orginallly I was trying to do it by passing values in a method, but I could not get that to work.  I would still like to learn to do it this way, is this a more efficient/better way?  Here is what I was trying:

*******FORM1 (JetBulls)********

string[] name = new string[15];
string[] myArray2;
myArray2 = Seats.passValuesMethod(myNewArray);

*******FORM2 (Seats)********
        public string[] passValuesMethod(string[] myNamesArray)
        {
            string[] myNewArray = new string[15];
            return myNewArray;
        }

***


Answers (6)