Deadlock
Deadlock

Reputation: 330

How to pass list values from one class to other form in C#?

Actually I have 3 forms, and one class , class(ReadXMLToEcasWindow) in which i defined the list

public List<string> ack_line_path = new List<string>();

From form 1 on button_click , form2 will popup, inside form2 i am adding values to List under the function

private void add_path_after_successful_load()
    {
        int rowcount = Ecas_config_gridview.Rows.Count;

        for (int i = 0; i < rowcount; i++)
        {
            XML_To_Ecas.ack_line_path.Add(Ecas_config_gridview.Rows[i].Cells[3].Value.ToString());
        }

        this.Hide();
    }

//once  the values got added form2 will be hidden,  again **clicking  on form1 button**  , form3 under which i want to use these list values

private void btn_ECAS_Click(object sender, EventArgs e)
{
    ECAS_WINDOW_FORM F_Ecas= new ECAS_WINDOW_FORM(this);
    F_Ecas.Show(); 
}

Upvotes: 2

Views: 2406

Answers (3)

Debajit Mukhopadhyay
Debajit Mukhopadhyay

Reputation: 4182

There are multiple option by which you can send the values to another form in windows application.

1) Setting up properties:

ECAS_WINDOW_FORM F_Ecas= new ECAS_WINDOW_FORM(this);
F_Ecas.ack_line_path = this.ack_line_path;
F_Ecas.Show(); 

2) Send by method which answered previously:

ECAS_WINDOW_FORM F_Ecas= new ECAS_WINDOW_FORM(this);
F_Ecas.setYourList(list);
F_Ecas.Show(); 

3) Build a static class which will hold the values. You can access the values of the static class throughout any where in the application:

static class Holder
{
   public static List<string> ack_line_path = new List<string>();
}

Set the holder value

ECAS_WINDOW_FORM F_Ecas= new ECAS_WINDOW_FORM(this);
Holder.ack_line_path = this.ack_line_path;
F_Ecas.Show(); 

Then access the holder value anywhere inside the application.

Upvotes: 2

sameh.q
sameh.q

Reputation: 1709

Many Ways to achieve this, simply the most obvious ways are:

A public method on Form 2 that you call it from the opener form after initializing form 2

Or, create another constructor for form 2 that accepts your list as input, and use this constructor when initializing form 2

Upvotes: 0

Anderson
Anderson

Reputation: 2758

What about an easy way:

 ECAS_WINDOW_FORM F_Ecas= new ECAS_WINDOW_FORM(this);
 F_Ecas.setYourList(list);
 F_Ecas.Show(); 

Upvotes: 0

Related Questions