R
R
Roma2018-06-15 22:40:36
Windows Forms
Roma, 2018-06-15 22:40:36

How to pass an array from one form to another?

Created 2 WinForms forms, I want to transfer an array from one form to the second.
To do this, I declared a pointer to an integer in a separate file, in the first form I fill it in, and in the second I want to display it.
The file in which the pointer is declared. Decl.h

#pragma once
#ifndef DECL
#define DECL

int *crit_arr;

#endif

Main form. MyForm.h
// 
#pragma once
#include "Decl.h"

// функция заполнения массива внутри класса MyForm
void get_Crit() {
      crit_arr[0] = crit_arr[3] = crit_arr[8] = 1;
      crit_arr[1] = Convert::ToInt32(textBox1->Text);
      crit_arr[2] = Convert::ToInt32(textBox2->Text);
      crit_arr[5] = Convert::ToInt32(textBox3->Text);
      crit_arr[3] = 1 / crit_arr[1];
      crit_arr[6] = 1 / crit_arr[2];
      crit_arr[7] = 1 / crit_arr[5];
    }
// Кнопка, по которой открывается вторая форма
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    crit_arr = new int[9];
    get_Crit();
    CritTable^ F2 = gcnew CritTable(this);
    F2->Show();
  }

Second form CritForm.h
#pragma once
#include "Decl.h" // ругается при наличии именно на этот include
// Конструктор, принимающий первую форму
public:
    CritTable(System::Windows::Forms::Form ^ frm)
    {
      InitializeComponent();
      mainform = frm;
    }
//  функция, которая вывод 1й элемент массива во второй форме.
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    int arr[9];
    arr[0] = crit_arr[0];
    textBox1->Text = Convert::ToString(arr[0]);
  }

In this situation, when compiling, I get an error:
C2065: crit_arr: undeclared identifier.
If you remove everything about the array from the function in the second form, then it throws an error:
LNK1169 and LNK2005 ...
how do you advise me to do it?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question