Pass list to another form without reloading it

ahmed omar 181 Reputation points
2022-10-29T12:32:45.893+00:00

Hi,
I have two Forms, Form A and Form B C# Winform application .

When the user click on the button in Form A , will show Form B , then Form B will generate a list of integer, this list should be sent to Form A (which is already opened) , and Form A has a button to print the list

FormA >> public List<int> MyArrayList = new List<int>();
Form A code to open Form B

var exm = new FormB();  
            exm.Show();  

Form B code to generate the List

 MyList.Add[1];  
 MyList.Add[4];  
 MyList.Add[5];  
 MyList.Add[6];  

Form B code to open Form A and send the Array

 var fm= new FormA();  
           fm.MyArrayList = this.MyArrayList ;  
            this.Close();  

But I received error

FormA.Mylist.**get** returned null  

will work only if I FormA.show() is used from FormB

so please how may I send the ArrayList from FormB to FormA without closing FormA and open it again.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,239 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2022-10-29T15:42:30.827+00:00

    This is easy using an event. In the following example, MainForm is the startup form and ChildForm will pass an array to MainForm.

    Full source

    MainForm

    namespace SendToMainForm  
    {  
        public partial class MainForm : Form  
        {  
            public MainForm()  
            {  
                InitializeComponent();  
            }  
      
            private void OpenChildFormButton_Click(object sender, EventArgs e)  
            {  
                ChildForm childForm = new() { Owner = this };  
      
                childForm.PassMonths += ChildFormOnPassMonths;  
                childForm.Show(this);  
                childForm.Top = Top;  
                childForm.Left = Left +  Width + 10;  
            }  
      
            private void ChildFormOnPassMonths(string[] months)  
            {  
                listBox1.Items.AddRange(months);  
            }  
        }  
    }  
    

    ChildForm

    namespace SendToMainForm  
    {  
        public partial class ChildForm : Form  
        {  
            public delegate void OnPassMonths(string[] monthNames);  
            public event OnPassMonths PassMonths;  
      
            public ChildForm()  
            {  
                InitializeComponent();  
            }  
      
            private void SendDataToMainFormButton_Click(object sender, EventArgs e)  
            {  
                var months = System.Globalization  
                    .DateTimeFormatInfo.CurrentInfo  
                    .MonthNames.Take(12).ToArray();  
      
                PassMonths?.Invoke(months);  
            }  
        }  
    }  
      
    

    255381-child.png

    For other types such as int

    namespace SendToMainForm  
    {  
        public partial class ChildForm : Form  
        {  
            public delegate void OnPassMonths(int[] monthNames);  
            public event OnPassMonths PassMonths;  
      
            public ChildForm()  
            {  
                InitializeComponent();  
            }  
      
            private void SendDataToMainFormButton_Click(object sender, EventArgs e)  
            {  
                PassMonths?.Invoke(new[] { 1, 2, 3 });  
            }  
        }  
    }  
    

    And

    namespace SendToMainForm  
    {  
        public partial class MainForm : Form  
        {  
            public MainForm()  
            {  
                InitializeComponent();  
            }  
      
            private void OpenChildFormButton_Click(object sender, EventArgs e)  
            {  
                ChildForm childForm = new() { Owner = this };  
      
                childForm.PassMonths += ChildFormOnPassMonths;  
                childForm.Show(this);  
                childForm.Top = Top;  
                childForm.Left = Left +  Width + 10;  
            }  
      
            private void ChildFormOnPassMonths(int[] data)  
            {  
                listBox1.Items.AddRange(Array.ConvertAll(data, x => x.ToString()));  
            }  
        }  
    }  
    

1 additional answer

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2022-10-29T13:18:41.763+00:00

    Do you close FormA when you open FormB? If not, rather than creating a new FormA from inside FormB why not pass in a reference to FormA into FormBs constructor and set it as a member variable? Then you'll be able to access the original form for the lifetime of the new form.