C# 从单个类读取多个类

C# 从单个类读取多个类,c#,winforms,C#,Winforms,我正在从事一个项目,该项目需要在加载Form1时启动计时器,该计时器在滴答声事件中增加类TimeCounter的TimerCount属性 该项目还有Form2,打开时,我想从TimeCounter类读取增量更新,该类由Form1递增,因为Form1是父级,不会关闭。我试图从TimeCounter读取增量更新,但得到的默认设置值为0 以下是代码: 定时器类 public class TimeCounter { public int timer=0; public int Timer

我正在从事一个项目,该项目需要在加载
Form1
时启动
计时器,该计时器在滴答声事件中增加类
TimeCounter
TimerCount
属性

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

以下是代码:

定时器类

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

    public int  GetTime()
    {        
        return timer;           
    }
}
格式1秒后的增量时间计数

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


我修改了你发布的代码如下。如果你不懂,那么你需要开始学习C

计时器:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}
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++;
    }        
}
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:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}
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++;
    }        
}
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();
            }));
        }
    }
}
表格2:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}
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++;
    }        
}
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();
            }));
        }
    }
}

我修改了你发布的代码如下。如果你不懂,那么你需要开始学习C

计时器:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}
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++;
    }        
}
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:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}
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++;
    }        
}
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();
            }));
        }
    }
}
表格2:

public class TimeCounter
{
    public static int timer = 0;
    public static int TimerCount 
    { 
        get
        {
            return timer;
        }
        set
        {
            timer = value;
        }
    }
}
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++;
    }        
}
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();
            }));
        }
    }
}

您没有将
计时器
对象正确传递到Form2。您需要将表单1使用的
计时器的实例传递给表单2

计时器:

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

    public int GetTime()
    {

        return timer;

    }
}
表格1:

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();
    }
}
输出:

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

计时器:

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

    public int GetTime()
    {

        return timer;

    }
}
表格1:

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();
    }
}
输出:
实际上,您不需要
计时器
类,也不需要
格式2中的
计时器

请参阅下面的代码


表格1



表格2



实际上,在
Form2

请参阅下面的代码


表格1



表格2




代码中的任何内容如何工作??每当计时器经过时,您就初始化计时器t。检查我的回答阿布希切克解决方案是什么?为什么
Form1\u Load
在表格2中?复制和粘贴错误?从哪里打开
Form2
?代码中的任何内容如何工作??每当计时器经过时,您就初始化计时器t。检查我的回答阿布希切克解决方案是什么?为什么
Form1\u Load
在表格2中?复制和粘贴错误?从何处打开
Form2
?您将得到一个交叉线程异常,因为您无法更新勾号中的标签event@Abhishek不,你不会的。请看屏幕截图。非常感谢Ephraim。您将获得一个交叉线程异常,因为您无法更新勾号中的标签event@Abhishek不,你不会的。请看截图。非常感谢Ephraim。表单接收计数器出现了一个小问题,它会延迟4,然后10,然后25…这是因为,两个表单有两个单独的计时器。根据您的问题,我了解到您想要更新
TimeCounter
类中的
TimerCount
变量,并在第二个窗体中的第二个计时器的计时器滴答声事件中获取该值。窗体接收计数器延迟时出现了一个小问题,它变为4,然后变为10,然后变为25…这是因为,2个表单有2个单独的计时器。根据您的问题,我理解的是,您希望更新
TimerCount
变量,并在第二个窗体中的第二个计时器的计时器勾选事件中获取该值。