Passing list data between forms in C#

65 Views Asked by At

I want to use the last member of the list of form1 in form2, but I can't. I tried several methods, but I did not get the result and it sends Null. eaxListSM.Last() and eayListSM.Last() SM is a acceleration signal which received from a gy-521 sensor.

and updated every 50 millisecods.

will I get the correct result if I use DataTransfer?

I also used the method of data and form reference.


namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        private SerialPort serialPort;

        // تعریف لیست ها
        public static List<double> axList = new List<double>();
        public static List<double> ayList = new List<double>();

        // تعریف تابع موج
        IEnumerable<double> eaxList;
        IEnumerable<double> eayList;
        IEnumerable<double> eazList;
        IEnumerable<int> eroundedAccResultList;
        IEnumerable<int> eroundedGyroResultList;
        public static IEnumerable<double> eaxListSM;
        public static IEnumerable<double> eayListSM;
        IEnumerable<double> eazListSM;
        IEnumerable<double> eaxListDF;
        IEnumerable<double> eayListDF;
        IEnumerable<double> eazListDF;

        public Form1()
        {
            InitializeComponent();
            serialPort = new SerialPort("COM3", 9600);
            serialPort.DataReceived += SerialPort_DataReceived;
            Basketball.Click += Basketball_Click;
            this.BackColor = Color.LightBlue;
            Form2 newForm = new Form2(this);

            eaxList = axList;
            eayList = ayList;
            eazList = azList;
            eroundedAccResultList = roundedAccResultList;
            eroundedGyroResultList = roundedGyroResultList;

            try
            {
                serialPort.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening serial port: " + ex.Message);
            }
        }

        private Form2 form2 = null;

        private void Basketball_Click(object sender, EventArgs e)
        {
 
            if (form2 == null || form2.IsDisposed)
            {
                Form2 form2 = new Form2(this);
                form2.FormClosed += (s, args) => form2 = null;
                form2.Show();
            }
            else
            {
                form2.Focus();
            }
        }
    }

    namespace Filtering
    {
        /// <summary>
        /// <para>Implements a Savitzky-Golay smoothing filter, as found in [1].</para>
        /// <para>[1] Sophocles J.Orfanidis. 1995. Introduction to Signal Processing. Prentice-Hall, Inc., Upper Saddle River, NJ, USA.</para>
        /// </summary>
        public sealed class SavitzkyGolayFilter
        {
            private readonly int sidePoints;

            private Matrix<double> coefficients;

            public SavitzkyGolayFilter(int sidePoints, int polynomialOrder)
            {
                this.sidePoints = sidePoints;
                Design(polynomialOrder);
            }

            /// <summary>
            /// Smoothes the input samples.
            /// </summary>
            /// <param name="samples"></param>
            /// <returns></returns>
            public double[] Process(double[] samples)
            {
                int length = samples.Length;
                double[] output = new double[length];
                int frameSize = (sidePoints << 1) + 1;
                double[] frame = new double[frameSize];

                Array.Copy(samples, frame, frameSize);

                for (int i = 0; i < sidePoints; ++i)
                {
                    output[i] = coefficients.Column(i).DotProduct(Vector<double>.Build.DenseOfArray(frame));
                }

                for (int n = sidePoints; n < length - sidePoints; ++n)
                {
                    Array.ConstrainedCopy(samples, n - sidePoints, frame, 0, frameSize);
                    output[n] = coefficients.Column(sidePoints).DotProduct(Vector<double>.Build.DenseOfArray(frame));
                }

                Array.ConstrainedCopy(samples, length - frameSize, frame, 0, frameSize);

                for (int i = 0; i < sidePoints; ++i)
                {
                    output[length - sidePoints + i] = coefficients.Column(sidePoints + 1 + i).DotProduct(Vector<double>.Build.Dense(frame));
                }

                return output;
            }

            private void Design(int polynomialOrder)
            {
                double[,] a = new double[(sidePoints << 1) + 1, polynomialOrder + 1];

                for (int m = -sidePoints; m <= sidePoints; ++m)
                {
                    for (int i = 0; i <= polynomialOrder; ++i)
                    {
                        a[m + sidePoints, i] = Math.Pow(m, i);
                    }
                }

                Matrix<double> s = Matrix<double>.Build.DenseOfArray(a);
                coefficients = s.Multiply(s.TransposeThisAndMultiply(s).Inverse()).Multiply(s.Transpose());
            }
        }

    }
}

namespace WindowsFormsApp3
{
    public partial class Form2 : Form
    {
        private int ballX = 50;
        private int ballY = 50;
        private int ballSpeedX = 0;
        private int ballSpeedY = 0;
        private DateTime basketEntryTime;

        private Form1 parentForm;

        public Form2(Form1 parentForm)
        {
            InitializeComponent();
            Timer timer = new Timer();
            timer.Interval = 50;
            timer.Tick += new EventHandler(Update);
            timer.Start();

            this.parentForm = parentForm;

        }

     
        private void Form2_Load(object sender, EventArgs e)
        {
            double eaxValue = parentForm.eaxListSM.Last();
            double eayValue = parentForm.eayListSM.Last();

        }
    }
}


0

There are 0 best solutions below