C# Kronometre Yapımı

S

Spectrum Artz

Web | Graphics
Ultra Member
Joined
Jul 31, 2019
Messages
525
Reaction score
111
C# Form ile Timer kullanarak kronometre yapımını gösteren örnek. Örnekte 2 adet button kullanılacaktır. Button1 kronometreyi başlatmak ve durdurdurmak için, Button2 ise Kronometreyi sıfırlamak için kullanılacaktır. Kronometre yapımında saat, dakika ve saniye değişkenlerini kullanacağız.

Burada ayarlamamız gereken şey, saniye 60′ a geldiğinde saniyeyi sıfırlamak ve dakikayı bir arttırmak olacaktır. Yine aynı şekilde dakika 60 olduğunda dakikayı sıfırlayarak saat değişkeninin içeriğini 1 arttıracağız.


Bu değişkenlerin içeriğini Form üzerine eklediğimiz Label üzerinde yazdıracağız. Timer kontrolünün Interval özelliğini 1000 olarak ayarladığımızda bu Timer’ ın 1000ms yani 1sn de bir çalışmasını sağlayacağız.

C# Timer kullanarak kronometre yapımına ait kodlar ve ekran çıktısı aşağıdaki şekilde olacaktır.



Code:
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 krono
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "BAŞLAT";
        }
 
        int durum = 0;
        int saat = 0, dakika = 0, saniye = 0;
 
        private void button2_Click(object sender, EventArgs e)
        {
            saat = 0;
            dakika = 0;
            saniye = 0;
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            
            if(saniye==60)
            {
                saniye = 0;
                dakika++;
                    
            }
            if (dakika == 60)
            {
                dakika = 0;
                saniye = 0;
                saat++;
            }
            label1.Text = String.Format("{0:D2}", saat)+":"+ String.Format("{0:D2}", dakika) + ":" + String.Format("{0:D2}", saniye);
            saniye++;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if(durum==0)
            {
                
                button1.Text = "DURDUR";
                durum = 1;
                timer1.Start();
            }
            else if(durum==1)
            {
                button1.Text = "BAŞLAT";
                durum = 0;
                timer1.Stop();
            }
        }
    }
}


kronometre.jpg
 
SPAM IS FORBIDDEN!
  • SPAMMERS ARE BANNED FROM THE FORUM AND CANNOT USE ANY OF THE CHEATS
  • For example: thanks, thx, very good, asdqwe, working, ty and so on!
  • For example: Writing the same message over and over. thanks, thx and so on!
  • Copying and copying someone else's message is prohibited.
  • It is forbidden to send messages to increase the number of comments on threads that you have no knowledge of.
  • Write your own opinion when commenting!
  • If you see spam message, please let us know with the REPORT button!

Tema düzenleyici

Top Bottom