首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在c# winform中使用计时器更新子窗体数组?

如何在c# winform中使用计时器更新子窗体数组?
EN

Stack Overflow用户
提问于 2022-08-21 08:08:27
回答 1查看 44关注 0票数 0

父窗体中的数组每100 milliseconds.

  • Once更新一次,其子窗体以父窗体的菜单形式打开,数据被传递给子窗体

发行:

到目前为止,我已经成功地传递了数组数据,但是我也需要在form.

表单中定期更新它,在子中设置计时器也有一些困难。

Form1:或父窗体

代码语言:javascript
运行
复制
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:
   .....
 }
代码语言:javascript
运行
复制
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:或Form /Pack1 1表格

public void TakeThis(string[] ArrayPack1Cells),方法复制文本框中的所有28个数组,但只复制一次。

代码语言:javascript
运行
复制
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];
  }
}

private void P1_timer_Tick(object sender, EventArgs e)根本不起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-21 12:05:50

这是我的想法。

父级:

代码语言:javascript
运行
复制
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);
        }
    }
}

包装:

代码语言:javascript
运行
复制
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];
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73432648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档