button to add a minute

Asked

Viewed 59 times

-4

Hello I have this code and I’d like to do a boot to add a minute to the time that’s running.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        private int quick = 1800;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1 = new System.Windows.Forms.Timer();
            timer1.Interval = 1800;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Enabled = true;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            quick--;
            label1.Text = quick / 60 + " : " + ((quick % 60) >= 10 ? (quick % 60).ToString() : "0" + (quick % 60));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }
    }
}
  • And what’s the problem?

  • Try to be more direct in your question, it is difficult to know how to help with very broad questions. If you have tried to do and have not succeeded there must be some more specific doubt or error that you do not know how to solve.

  • @Cesargoulart managed to solve his problem?

  • I wanted to put a boot to add a minute to the time, what code do I put in the boot to work? obg

1 answer

0


The interval of your timer is wrong, if you want a synchronization with seconds, must match with that interval, as it is assigned in milliseconds and for your event Tick is fired every 1s (by decreasing the remaining time), you must declare its value as 1000 (1000ms = 1s). The exhibition part, on the other hand, is a little confused and with unnecessary operations.

To increase the remaining time, simply increase the value at the click of the button, in case, I added the button3 to add 60 seconds.

And finally, you are forgetting to stop your timer after the end of the desired interval.

public partial class Form1 : Form
{
    int tempoRestante = 1800;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1 = new System.Windows.Forms.Timer();
        timer1.Interval = 1000;
        timer1.Tick += new EventHandler(timer1_Tick);            
        timer1.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {

        if (tempoRestante <= 0)
            timer1.Stop();
        else
            tempoRestante--;

        int minutos = tempoRestante / 60;
        int segundos = tempoRestante % 60;

        label1.Text = string.Format("{0}:{1}", minutos.ToString("#,00"), segundos.ToString("#,00"));

    }

    private void button3_Click(object sender, EventArgs e)
    {
        tempoRestante += 60;

    }
}
  • Thank you this solved. Thank you Leandro Angelo

  • @Cesargoulart Was that really the problem? If the proposal is the answer to your question, it is important to vote as useful and accept as the answer, so your question and the solution can be useful to other users who might face the same problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.