- The array in the parent form is updated every 100 milliseconds.
- Once the child form is opened with menustrip in parent form, the data is passed to child form
Issue:
- Till now, I have had success in passing the array data, but I need to update it periodically in the
child
form also, and I have some difficulties with setting the timer in child form.
Form1: or Parent Form
string[] ArrayPack1Cells = new string[28];
//All the values are first stored in 2D array `volt_info_array[packidindex, voltage_index]`
//and after scaling it, elements store in 1D array depending on the `packidindex` value
Voltages = ((volt_info_array[packidindex, voltage_index] / 1000));
switch(PackIndexes)
{
case 1:
// if size is 28, then convert to array to be passed to child form.
if(ListPack1Cells.Count == 28)
{
ArrayPack1Cells = ListPack1Cells.ToArray();
}
break;
case 2:
.....
}
private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem menu = sender as ToolStripMenuItem;
switch (menu.Name)
{
case "pack1ToolStripMenuItem":
if (Application.OpenForms["Pack1"] is Pack1 pack1)
{
pack1.Focus();
return;
}
pack1 = new Pack1();
pack1.TakeThis(ArrayPack1Cells);
pack1.MdiParent = this;
pack1.Show();
Array.Clear(ArrayPack1Cells, 0, ArrayPack1Cells.Length);// Clear it once send to form2
break;
}
public void TakeThis(string[] ArrayPack1Cells)
, method copies all the 28 arrays in the texboxes but only once.
public List<Control> Cell_Volt1 = new List<Control>();
public string[] f_temp = new string[28];
public Pack1()
{
InitializeComponent();
Cell_tbxArray();
if (P1_timer.Enabled == false)
{
P1_timer.Enabled = true;
P1_timer.Tick = new System.EventHandler(this.P1_timer_Tick);
P1_timer.Start();
}
else if (P1_timer.Enabled)
{
1_timer.Stop();
P1_timer.Enabled = true;
P1_timer.Start();
}
}
private void Cell_tbxArray()
{
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i )
{
if (tableLayoutPanel1.Controls[i].GetType() == typeof(TextBox))
{
Cell_Volt1.Add(tableLayoutPanel1.Controls[i]);
}
}
}
public void TakeThis(string[] ArrayPack1Cells)
{
f_temp = ArrayPack1Cells;
int index = 0;
foreach (string item in f_temp)
{
Cell_Volt1[index].Text = item;
index ;
}
}
private void P1_timer_Tick(object sender, EventArgs e)
{
for (int i = 0; i < Cell_Volt1.Count; i )
{
Cell_Volt1[i].Text = f_temp[i];
}
}
The private void P1_timer_Tick(object sender, EventArgs e)
isnt working at all.
CodePudding user response:
Here is my take.
Parent:
public static class Consts
{
public const int NPACKS = 10, NVOLTS = 28;
}
public partial class ParentForm : Form
{
double[,] data = new double[Consts.NPACKS, Consts.NVOLTS];
double[][] uidata = new double[Consts.NPACKS][];
Dictionary<int, PackForm> forms = new Dictionary<int, PackForm>();
public ParentForm()
{
InitializeComponent();
for (int i = 0; i < Consts.NPACKS; i )
{
uidata[i] = new double[Consts.NVOLTS];
}
}
// wild guess - not clear how do you update it - all of them or slices
void DataContinuousUpdate(double value, int npack, int nvolt)
{
data[npack, nvolt] = value;
var slice = uidata[npack];
// in case a form is trying to refresh UI
lock (slice)
slice[nvolt] = value;
}
void OpenPack(int npack)
{
// assuming access to this method is serial
if (forms.ContainsKey(npack))
return;
var slice = uidata[npack];
lock (slice)
{
var form = new PackForm(npack, slice);
forms.Add(npack, form);
}
}
}
Pack:
public partial class PackForm : Form
{
public int ID { get; private set; }
public double[] Data { get; private set; }
public double ValueScale { get; set; }
TextBox[] VoltTextBox = new TextBox[Consts.NVOLTS];
Timer timer;
private PackForm()
{
InitializeComponent();
CreateTextBoxes();
ValueScale = 1000.0;
this.FormClosing = PackForm_FormClosing;
}
public PackForm(int iD, double[] data) : this()
{
InitializeComponent();
ID = iD;
Data = data;
StartTimer();
}
private void PackForm_FormClosing(object? sender, FormClosingEventArgs e)
{
StopTimer();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void StartTimer()
{
StopTimer();
timer = new Timer();
timer.Enabled = true;
timer.Interval = 1000;
timer.Tick = Timer_Tick;
}
private void StopTimer()
{
if (timer == null) return;
timer.Enabled = false;
timer.Tick -= Timer_Tick;
}
private void Timer_Tick(object? sender, EventArgs e)
{
if (Data == null) return;
lock(Data)
{
for (int i = 0; i < Data.Length; i )
{
VoltTextBox[i].Text = Data[i];
}
}
}
}