简体   繁体   English

如何在 c# winform 中使用计时器更新子窗体的 arrays?

[英]How to update the arrays of childforms with timer in c# winform?

  1. The array in the parent form is updated every 100 milliseconds.父表单中的数组每 100 毫秒更新一次。
  2. Once the child form is opened with menustrip in parent form, the data is passed to child form一旦在父表单中使用 menustrip 打开子表单,数据就会传递给子表单

Issue:问题:

  1. 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.到目前为止,我已经成功传递了数组数据,但是我还需要在child表单中定期更新它,并且在子表单中设置计时器时遇到了一些困难。

Form1: or Parent Form Form1:或父表格

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;
}

Form2: or Child/Pack1 Form Form2:或 Child/Pack1 Form 在此处输入图像描述

public void TakeThis(string[] ArrayPack1Cells) , method copies all the 28 arrays in the texboxes but only once. public void TakeThis(string[] ArrayPack1Cells) ,方法复制texboxes中的所有28个arrays,但只复制一次。

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. private void P1_timer_Tick(object sender, EventArgs e)根本不起作用。

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];
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM