我写的基本Windows窗体应用程序有一些代码。但出了点问题。我正在启动应用程序,一切正常,但如果我启动计时器,它首次运行并且不会重复。我该怎么办?
抱歉语法错误。
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.Timers;
using System.Windows.Forms;
namespace Zamanlayıcı
{
public partial class Form1 : Form
{
public int saat;
public int dakika;
public int saniye;
public int zaman;
public Form1()
{
InitializeComponent();
}
private void baslat_button_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
private void durdur_button_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void demizle_button_Click(object sender, EventArgs e)
{
saat_box.Clear();
dakika_box.Clear();
saniye_box.Clear();
zaman_text.Text = "00:00:00";
}
private void ses_checkbox_CheckedChanged(object sender, EventArgs e)
{
}
private void timer1_Tick_1(object sender, EventArgs e)
{
int saat = int.Parse(saat_box.Text);
int dakika = int.Parse(dakika_box.Text);
int saniye = int.Parse(saniye_box.Text);
int zaman = saat * 60 * 60 + dakika * 60 + saniye;
int saat2 = zaman / 60 / 60;
int dakika2 = zaman / 60 % 60;
int saniye2 = zaman % 60;
if(zaman > 0) {
zaman--;
zaman_text.Text = saat2.ToString("00") + ":" + dakika2.ToString("00") + ":" + saniye2.ToString("00");
}if(zaman == 0)
{
MessageBox.Show("Zaman doldu!", "Bitti", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
答案 0 :(得分:4)
实际上计时器运行并且每秒都会调用timer1_Tick_1
。问题是您在zaman
中有一个局部变量timer1_Tick_1
,它始终具有相同的值saat * 60 * 60 + dakika * 60 + saniye;
,因此您始终可以看到该初始值。删除局部变量,只使用字段zaman
;在计时器启动时进行初始计算:
public partial class Form1 : Form {
public int saat;
public int dakika;
public int saniye;
public int zaman;
public Form1() {
InitializeComponent();
}
private void baslat_button_Click(object sender, EventArgs e) {
timer1.Interval = 1000;
int saat = int.Parse(textBox1.Text);
int dakika = int.Parse(textBox2.Text);
int saniye = int.Parse(textBox3.Text);
zaman = saat * 60 * 60 + dakika * 60 + saniye;
timer1.Start();
}
private void durdur_button_Click(object sender, EventArgs e) {
timer1.Stop();
}
private void demizle_button_Click(object sender, EventArgs e) {
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Text = "00:00:00";
}
private void ses_checkbox_CheckedChanged(object sender, EventArgs e) {
}
private void timer1_Tick_1(object sender, EventArgs e) {
int saat2 = zaman / 60 / 60;
int dakika2 = zaman / 60 % 60;
int saniye2 = zaman % 60;
if (zaman > 0) {
zaman--;
textBox4.Text = saat2.ToString("00") + ":" + dakika2.ToString("00") + ":" + saniye2.ToString("00");
}
if (zaman == 0) {
MessageBox.Show("Zaman doldu!", "Bitti", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
答案 1 :(得分:1)
就像Alex所说的那样,你在tick事件处理程序中声明zaman
,所以每次滴答(每秒),它只会重置该值。您应该在tick事件处理程序之外声明变量,并在其中修改它们。
public partial class Form1 : Form
{
public int saat;
public int dakika;
public int saniye;
public int zaman;
public Form1()
{
InitializeComponent();
}
private void baslat_button_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
int saat = int.Parse(textBox1.Text);
int dakika = int.Parse(textBox2.Text);
int saniye = int.Parse(textBox3.Text);
zaman = saat * 3600 + dakika * 60 + saniye; //You were multiplying by 60 2x - just multiply by 3600
timer1.Start();
}
private void timer1_Tick_1(object sender, EventArgs e)
{
int saat2 = zaman / 3600; //You were dividing by 60 2x - just divide by 3600
int dakika2 = zaman / 60 % 60;
int saniye2 = zaman % 60;
if (zaman > 0)
{
zaman--;
textBox4.Text = saat2.ToString("00") + ":" + dakika2.ToString("00") + ":" + saniye2.ToString("00");
}
if (zaman == 0) {
MessageBox.Show("Zaman doldu!", "Bitti", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}