drew

drew

  • NA
  • 12
  • 0

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

Nov 2 2009 10:10 AM
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)

0
drew

drew

  • 0
  • 12
  • 0
Nov 2 2009 5:56 PM
Sam,

Thanks for your response.  I tried some of the options you mentioned and in the process it made me re-look at the code from another perspective.  I realize that I was initializing the second form inside of the button click event for form2.  So every time I clicked to open it, it was re initializing the form.

I did like your suggestion about hiding the form and i'm going to use that as part of my solution.

Thanks for your help!
0
Sam Hobbs

Sam Hobbs

  • 54
  • 29.3k
  • 2.1m
Nov 2 2009 4:39 PM
I don't think you understand. I don't understand what you mean by "pass the values from form2 back to form1" but there are solutions to that problem. One possible solution is that in whatever code you use to show the form when it is created every time, you could instead call code in the form to be shown to do whatever is done in the form load, except only what needs to be done. Another possibility is to use the form activation (OnActivated) event; the documentation indicates that for .Net we need to use the ActiveForm property of the page in order to get the Activate Event. I think that the "trick" of showing and hiding a window is more common than you realize.

Another common "trick" is to use a document/object separate from the form. A common term for this is "Business object", as in:
http://en.wikipedia.org/wiki/Business_object_(computer_science)

You can have a reference to your business object(s) in your application class so it is (or they are) accessible to all forms. I don't know what the common way to do that is; I asked and did not get answers so I figured out how to do it.
0
Kirtan Patel

Kirtan Patel

  • 0
  • 21.9k
  • 4.1m
Nov 2 2009 3:24 PM
Hey  Friend ,

Can you mail Me your Project Zip in Mail

so that i can understand your problem better :) because i m still in confusion what you doin with that check Boxes :)

so if you will send me i can resolve it Quickly :)

my email Add : kirtan007@gmail.com

Thank you Friend :)
0
drew

drew

  • 0
  • 12
  • 0
Nov 2 2009 3:18 PM
Sam,

A good thought, but I don't think that will work in this case because I still need to pass the values from form2 back to form1 so they can be displayed on certain button clicks.



Hi Kirtan,

Thanks for showing me how to do it the other way as well.

However I still have the problem with the dialog box.  It pops up everytime the user opens form2 because the values are read in on the form2_Load event.  I only want the dialog box to show up when the user clicks on a check box in form 2 to mark it as checked.

Here is the form2_Load code with your new method for passing with funcion:

public void Seats_Load(object sender, EventArgs e)
        {
            if (myNewArray[0] == "seated")
                chkBox1.Checked = true;
            if (myNewArray[1] == "seated")
                chkBox2.Checked = true;
        }

0
Kirtan Patel

Kirtan Patel

  • 0
  • 21.9k
  • 4.1m
Nov 2 2009 2:11 PM
 Friend,

Here is Coded how to do in your Way with function :)

friend ,dont forget to mark "Do you like this answer" if it helps you :)

Form1 Code
-------------


 private void Form1_Load(object sender, EventArgs e)

        {

            string[] form1array = new string[5];

            form1array[0] = "test";

            form1array[1] = "test1";

            form1array[2] = "test2";

            form1array[3] = "test3";

            form1array[4] = "test4";

 

            Form2.passValuesMethod(form1array);

        } 


Form 2 Code
--------------

        public static string[] myNewArray;

 

        private void Form2_Load(object sender, EventArgs e)

        {

            //Use the Retrived Array MyNewArray

            foreach (string str in myNewArray)

            {

                listBox1.Items.Add(str);

            }

        }

 

         public static void passValuesMethod(string[] myNamesArray)

         {

            myNewArray = myNamesArray;

         }


0
Sam Hobbs

Sam Hobbs

  • 54
  • 29.3k
  • 2.1m
Nov 2 2009 1:25 PM
I have not looked at your code closely but I will suggest an easy solution in case that helps. Can you simply hide/show the form? Then you don't need to load and unload the form. Is that a possible solution?