NEWS

[C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform

[C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
Đăng bởi: Thảo Meo - Lượt xem: 2945 09:14:57, 17/02/2023C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình hướng dẫn các bạn cách sử dụng Open AI Chat GPT hỗ trợ trong việc lập trình C#, winform.

Ở bài viết này mình yêu cầu nó viết cho mình một ứng dụng count down timer với 60s.

Vậy count down timer là gì?

Countdown timer là một công cụ rất hữu ích trong lập trình ứng dụng C# để đếm ngược thời gian từ một giá trị bắt đầu cho đến khi đạt được một giá trị kết thúc nhất định.

Countdown timer có thể được sử dụng trong nhiều tình huống khác nhau, từ đếm ngược thời gian để kích hoạt một tác vụ đến thông báo người dùng về thời gian còn lại trước khi một sự kiện quan trọng diễn ra.

Trong ứng dụng C#, để sử dụng countdown timer, chúng ta có thể sử dụng lớp Timer có sẵn trong namespace System.Timers.

Để bắt đầu sử dụng Timer, chúng ta cần tạo một thể hiện của lớp này bằng cách sử dụng constructor mặc định, sau đó thiết lập các thuộc tính của Timer như Interval, Enabled, và event handler để xử lý các sự kiện.

Dưới đây là một ví dụ về cách sử dụng countdown timer trong ứng dụng C# để đếm ngược từ 60 giây cho đến khi đạt được giá trị kết thúc 0 giây:

count_down_thumb_csharp

Video hướng dẫn các bước thực hiện Step by Step:

Full source code C#:

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 TestCountDownTimer
{
    public partial class Form1 : Form
    {
        private DateTime startTime;
        private Timer timer2;
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Paint += PictureBox1_Paint;
        }

        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            if (timer2.Enabled == false)
            {
                // Draw the border of the circle
                using (Pen pen = new Pen(Color.Red, 20))
                {
                    g.DrawEllipse(pen, 10, 10, 230, 230);
                }
            }
            else {
                // Draw the border of the circle
                using (Pen pen = new Pen(Color.White, 20))
                {
                    g.DrawEllipse(pen, 10, 10, 230, 230);
                }
            }
           

            // Draw the countdown arc
            int remainingSeconds = 60 - (int)(DateTime.Now - startTime).TotalSeconds;
            if (remainingSeconds < 0)
            {
                remainingSeconds = 0;
            }
            float angle = (float)remainingSeconds / 60 * 360;
            using (Pen pen = new Pen(Color.Red, 20))
            {
               g.DrawArc(pen, 10, 10, 230, 230, -90, angle);
            }

            // Draw the remaining time in the center of the circle
            string remainingTime = remainingSeconds.ToString();
            using (Font font = new Font("Tahoma", 48, FontStyle.Bold))
            {
                SizeF size = g.MeasureString(remainingTime, font);
                PointF point = new PointF((250 - size.Width) / 2, (250 - size.Height) / 2);
                Font font2 = new Font("Tahoma", 18);
                     SizeF size2 = g.MeasureString("SECONDS", font2);

                PointF point2 = new PointF((250 - size2.Width) / 2 , (250 - size2.Height) / 2 + 45) ;
                using (Brush brush = new SolidBrush(Color.Black))
                {
                    g.DrawString(remainingTime, font, brush, point);
                    g.DrawString("SECONDS", font2, brush, point2);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer2 = new Timer();
            timer2.Interval = 1000;
            timer2.Tick += Timer2_Tick;

            // Set the PictureBox size to 250 x 250 pixels and set its BackColor to white
            pictureBox1.Size = new Size(250, 250);
            pictureBox1.BackColor = SystemColors.Control;

        }

        private void Timer2_Tick(object sender, EventArgs e)
        {
            // Calculate the remaining seconds
            int remainingSeconds = 60 - (int)(DateTime.Now - startTime).TotalSeconds;
            // Redraw the PictureBox
            pictureBox1.Invalidate();
            if (remainingSeconds < 0)
            {
                remainingSeconds = 0;
                timer2.Stop();
            }

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Set the start time to the current time
            startTime = DateTime.Now;

            // Start the timer
            timer2.Start();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
Đăng bởi: Thảo Meo - Lượt xem: 2945 09:14:57, 17/02/2023C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.