从单个类读取多个类

时间:2017-06-09 05:33:40

标签: c# winforms

我正在开展一个项目,该项目要求Timer加载Form1,以便在tick事件中增加TimerCountTimeCounter属性。< / p>

项目还有Form2,当我打开时,我想要读取TimeCounter类的增量更新,该增量更新由Form1递增,因为Form1是父级,并且将会不接近我试图从TimeCounter读取,但得到默认设置值为0。

这是代码:

计时器类

public class TimeCounter
{
    public int timer=0;
    public int TimerCount { get; set; }

    public int  GetTime()
    {        
        return timer;           
    }
}

Form1在1秒后递增TimerCount

private void Form1_Load(object sender, EventArgs e)
{
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;
    timer.Elapsed += timer_Elapsed;
    timer.Start();
}

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    sk++;
    Timer t = new Timer();
    t.TimerCount = sk;

}

继续(但不工作)接收计数器的Form2

  private void Form1_Load(object sender, EventArgs e)
  {
      System.Timers.Timer timer = new System.Timers.Timer();
      timer.Interval = 1000;
      timer.Elapsed += timer_Elapsed;
      timer.Enabled = true;
      timer.Start();
  }

  void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
      Timer t1 = new Timer();
      B01CountDown.Text = t1.GetTime().ToString();           
  }

3 个答案:

答案 0 :(得分:1)

我修改了您发布的代码,如下所示。如果你不明白,那么你需要开始学习C#。

<强> TimeCounter:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}

<强> Form1中:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        new Form2().Show();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        TimeCounter.TimerCount++;
    }        
}

表格2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Elapsed += timer_Elapsed;
        timer.Enabled = true;
        timer.Start();
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (B01CountDown.InvokeRequired)
        {
            B01CountDown.Invoke((MethodInvoker)(() =>
            {
                B01CountDown.Text = TimeCounter.TimerCount.ToString();
            }));
        }
    }
}

答案 1 :(得分:1)

您没有正确地将Timer对象传递给Form2。您需要将表单1使用的Timer实例传递给表单2.

计时器:

public class Timer
{
    public int timer = 0;
    public int TimerCount { get; set; }

    public int GetTime()
    {

        return timer;

    }
}

Form1中:

public partial class Form1 : Form
{
    private Timer _timer;

    public Form1()
    {
        InitializeComponent();
        _timer = new Timer();
        timer1.Start();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Tick += timer1_Tick;
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        _timer.TimerCount++;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2(_timer);
        frm2.ShowDialog();
    }
}

窗体2:

public partial class Form2 : Form
{
    public Timer _timer;

    public Form2(Timer timer)
    {
        InitializeComponent();
        _timer = timer;
        timer1.Start();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        timer1.Tick += timer1_Tick;
    }

    void timer1_Tick(object sender, EventArgs e)
    {
        label1.Text = _timer.TimerCount.ToString();
    }
}

输出: enter image description here

答案 2 :(得分:1)

实际上,您不需要TimeCounter课程,Timer

} Form2

见下面的代码

  

Form1中

public partial class Form1 : Form
{
    int sk = 0;
    Form2 form2 = new Form2();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        System.Timers.Timer timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Elapsed += timer_Elapsed;
        timer.Start();
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        form2.Show();
    }

    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        // Do your Stuff
        sk++;
        form2.UpdateLabel(sk.ToString());
    }
}
  

窗体2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public void UpdateLabel(string Message)
    {
        if (B01CountDown.InvokeRequired)
        {
            B01CountDown.Invoke((MethodInvoker)(() =>
            {
                B01CountDown.Text = Message;
            }));
        }
    }
}