Имею класс с некоторыми полями:
public class MicrocontrollerControl
{
public string name;
public short[] Ports = new short[8];
public string description;
}
Инициализирую его в родительской форме:
public MicrocontrollerControl microcontrollerControl = new MicrocontrollerControl();
Заполняю созданный класс в событии двойного клика на грид и вызываю новую форму:
private async void dGView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int cind = e.ColumnIndex;
microcontrollerControl.description = null;
if (cind == 1)
{
string id = dGView1.Rows[dGView1.CurrentRow.Index].Cells[dGView1.CurrentCell.ColumnIndex].Value.ToString().Substring(13);//считываем id микроконтроллера и обрезаем чтобы получилось имя файла
string md = Environment.GetFolderPath(Environment.SpecialFolder.Personal);//путь к Документам
if (Directory.Exists(md + "\\PS_Aide") == false)
{
Directory.CreateDirectory(md + "\\PS_Aide");//создаем папку в документах
}
md += "\\PS_Aide" + "\\"+id+".txt";
FileStream fstream = null;
try
{
fstream = new FileStream(md, FileMode.Open);
// выделяем массив для считывания данных из файла
byte[] buffer = new byte[fstream.Length];
// считываем данные
await fstream.ReadAsync(buffer, 0, buffer.Length);
// декодируем байты в строку
// string textFromFile = Encoding.GetEncoding(1251).GetString(buffer);//ncoding.GetEncoding(1251)
byte countPort = 0;
for (int i = 0; i < buffer.Length; i++)
{
byte type = 0;
byte Length = 0;
if (buffer[i]==':')
{
type = (byte)(buffer[++i].MyAsciiToHex()<<4);
type += buffer[++i].MyAsciiToHex();
Length = (byte)(buffer[++i].MyAsciiToHex() << 4);
Length += buffer[++i].MyAsciiToHex();
byte[] buff = new byte[Length];
if (type== 0x01)//00 имя
{
for (byte j=0; j < Length; j++)
{
++i;
microcontrollerControl.name += ((char)buffer[i]);
}
}
if (type == 0x02 || type == 0x03 || type == 0x04 || type == 0x05 || type == 0x06 || type == 0x07 || type == 0x08 || type == 0x09)//PortABCDEFGH
{
short temp = 0;
byte ofs = 12;
for (byte j = 0; j < Length; j++)
{
temp |= (short)((buffer[++i].MyAsciiToHex()) << ofs);
ofs -= 4;
}
microcontrollerControl.Ports[countPort] = temp;
++countPort;
}
if (type == 0xff)//text
{
byte[] size = new byte[Length];
for (byte j = 0; j < Length; j++)
{
++i;
size[j] += ((byte)buffer[i]);
}
microcontrollerControl.description += Encoding.GetEncoding(1251).GetString(size);
}
}
}
ControlMicrocontrolersForm controlMicrocontrolersForm = new ControlMicrocontrolersForm();
controlMicrocontrolersForm.Owner = this;//устанавливаем владельца
controlMicrocontrolersForm.Show();
И вот здесь при получении данных выдает ошибку(
public partial class ControlMicrocontrolersForm : Form
{
public ControlMicrocontrolersForm()
{
InitializeComponent();
Form1 mainForm = this.Owner as Form1;
listBox1.Items.Add( mainForm.microcontrollerControl.name + '\n');//вот здесь получаю ошибку
for (int i = 0; i <= 15; i++)
{
DataGridStm.Rows[DataGridStm.Rows.Add()].Cells[0].Value = "Pin"+i;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
