简体   繁体   English

尝试以C#中的形式更改计时器的输出

[英]Trying to change output for a timer in a form in c#

I need to make a form in C# have a timer and have a label that will have be the display of the timer. 我需要在C#中使表单具有计时器,并具有将成为计时器显示的标签。 The label will need to be a generic label at first saying it is a counter, but when the timer starts it needs to display the count. 该标签首先需要是一个通用标签,说它是一个计数器,但是当计时器启动时,它需要显示计数。 Currently I have the display as a number up down but, it needs to be the control that can tweak the count, which it does. 目前,我将显示的数字调小了,但是它需要是可以调整计数的控件,它确实可以做到。 It just can't be the sole counter. 它只是不能成为唯一的计数器。

Here is my assignment: 这是我的作业:

Create a Windows Application. 创建一个Windows应用程序。 In the main form, create a label named “lTickCount”. 在主窗体中,创建一个名为“ lTickCount”的标签。 Create a timer named “tPeriodic”, and a numerical control of your own choosing. 创建一个名为“ tPeriodic”的计时器,并选择一个数字控件。 Each time the timer “ticks” increment an integer, display the integer value as a string in lTickCount. 每次计时器“滴答”增加一个整数时,在lTickCount中将该整数值显示为字符串。 Use the numerical control to change the update rate of the timer interactively. 使用数字控件以交互方式更改计时器的更新速率。

I think I have done everything correctly except for the bold part. 我认为我已经正确地完成了所有工作,除了粗体部分。 To finish I tried to make a string in both the label and the counter. 最后,我尝试在标签和柜台上都加上一个字符串。 I know I shouldn't have in both, I just wanted to show you the two things I've tried to help get better feedback: 我知道我不应该两者兼有,我只是想向您展示我试图帮助获得更好反馈的两件事:

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Text = "AAAAAAAA AAAAAAAA ########";
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                TickCounter.Text = "The timer has started";
                tPeriodic.Enabled = true;
            }
            else
            {
                TickCounter.Text = "The timer has ended";
                tPeriodic.Enabled = false;
            }

        }

        private void TickCounter_ValueChanged(object sender, EventArgs e)
        {
            TickCounter.Text = TickCounter.Value.ToString();
        }

        private void tPeriodic_Tick(object sender, EventArgs e)
        {
            TickCounter.Value += 1;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            TickCounter.Text = TickCounter.Value.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Can someone help me figure out what I'm doing wrong and point me in the right way? 有人可以帮助我弄清楚我做错了什么,并以正确的方式指出我吗?

If you are going to try to add to a string (A label value) you need to convert it to an Integer first. 如果要尝试添加到字符串(标签值),则需要先将其转换为Integer。

A couple ways to do this: 有几种方法可以做到这一点:

TextCount.Text = (Convert.ToInt32(TextCount.Text) + 1).ToString();

is one way, of course you could still use your += or any other math syntax for basic addition of +1. 是一种方法,当然您仍然可以使用+ =或任何其他数学语法来基本加+1。

You can also use tryParse, and in fact this should probably be used to verify you have an integer in the first place: 您还可以使用tryParse,实际上,这可能应该首先用于验证您拥有整数:

   int count;
   if (int.TryParse(TextCount.Text, out count))
        {
            count++;
            TextCount.Text = count.ToString();
        }
int count;
int tmrInterval = 1000; //1 sec
private void tPeriodic_Tick(object sender, EventArgs e)
{
    count++;
    lTickCount.Text = count.ToString();
}

private void TickCounter_ValueChanged(object sender, EventArgs e)
{
    if (TickCounter.Value == 0)
    {
        return; // or stop the timer
    }
    tPeriodic.Interval = TickCounter.Value * tmrInterval;
}

tPeriodic.Interval is the time till next tick in milliseconds. tPeriodic.Interval是到下一个刻度的时间(以毫秒为单位)。 You are updating the timer interval according to tmrInterval and the value of the numeric control. 您正在根据tmrInterval和数字控件的值更新计时器间隔。 You can change the interval or the formula i wrote to your own. 您可以更改时间间隔或我自己编写的公式。

valter 瓦尔特

Ok I found that: 好的,我发现:

tPeriodic.Interval = 1000 / Convert.ToInt32(TickCounter.Value * TickCounter.Value); tPeriodic.Interval = 1000 / Convert.ToInt32(TickCounter.Value * TickCounter.Value);

seemed to work in the numericupdown class. 似乎在numericupdown类中起作用。

Thanks for the help. 谢谢您的帮助。

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

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