I have searched high and low and cannot find anything that I understand on how to pass data from one form to another in c#.

I am completely new to this language and I felt like I was really grasping it up until now.

The situation:
I have a project that has multiple forms.
On the last form, I would like to use data that was collected in the various, previous forms. Some data is from listboxes and other data is from checkboxes. This data will be displayed in either labels (data from the listboxes) or a listbox (data from the checkboxes).

Is there a tutorial anywhere that will show me how to get that data to display in labels and listboxes and/or store it in variables on my final form? And is this tutorial in plain English so that I can follow along without getting totally lost?

Thanks in advance.

Maybe I am just ignorant, but that seems to be ASP.NET and I am not sure if it applies to what I am doing?

Ah very true. I guess you are dealing with Window Forms?

Yes. WinForms. At this point, I have been able to pass (some of)the data, but the form it goes to comes up immediately and I don't want that to happen.

You can create a seperate custom class which will hold the data. In any of the form you must have a reference (which has to be instantiated only one - on beginning) to this class; so you can access to the data on the class. The data can be in some properties, or in some collections line generic list.


Example:

class DataClass
{
    public List<string> list;
    pubic DataClass
    {
        list = new List<string>();
    }
}

class Form1 //create and set data
{
    Form2 f2;
    void Method1() //can be a button_Click event
    {
        //open form2 and pass data
        DataClass dc = new DataClass();
        dc.list.Add("Some text 1");
        dc.list.Add("Some text 2");
        if(f2 == null)
             f2 = new Form2(dc);  //passing reference of dataClass to other form
    }
}

class Form2 //set data
{
    Form3 f3;
    DataClass dc;
    public Form3(DataClass _dc)
    {
         this.dc = _dc;
    }

    void Method2() //can be a button_Click event
    {
        //open form3 and pass data        
        dc.list.Add("Some text 3");
        dc.list.Add("Some text 4");
        if(f3 == null)
             f3 = new Form3(dc);  //passing reference of dataClass to other form
    }
}

class Form3 // get data
{
     DataClass dc;
     public Form3(DataClass _dc)
     {
          this.dc = _dc;
     }
     
     void Method3()
     {
         // get the data from data class
         
         foreach(string str in dc.list)
         {
              //each str value has a value from the list
              string text = str;                  
         }
     }
}

This is only one of examples how to add data on forms, and get the data back out.

go through delegates, it would surely help you out.
you can call a method in one form with data and pass the data to other form with security using a delegate.
pls go through few links on delegates and events in c sharp.

Thanks guys. I got it all figured out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.