NEWS

[C#] Number Effect Counter up and down in winform

[C#] Number Effect Counter up and down in winform
Đăng bởi: Thảo Meo - Lượt xem: 4626 07:40:07, 12/12/2022DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ các bạn tạo hiệu ứng khi thay đổi số up and down trên lập trình c#, winform.

[C#] How to Number Effect Counter Up and Down in Winform

Giao diện demo ứng dụng:

effect_number

Ở demo trên, khi chúng ta thay đổi giá trị số sẽ chạy hiệu ứng từ từ.

Video demo Number Effect:

Ở bài viết này mình sử dụng yield return để thực hiện.

Source code c#:

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

namespace EffectNumber
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int oldValue;
        private void Form1_Load(object sender, EventArgs e)
        {
            oldValue = Convert.ToInt32(lblNumber.Text);
        }
        private IEnumerable<int> CountText(int newValue, int speed)
        {
            var CountFPS = 30;         
            var Duration = 1;
            int previousValue = oldValue;
            int stepAmount;
            oldValue = newValue; 

            if (newValue - previousValue < 0)
            {
                stepAmount = Convert.ToInt32( Math.Floor(Convert.ToDouble( (newValue - previousValue) / (CountFPS * Duration) ))); 
            }
            else
            {
                stepAmount = Convert.ToInt32( Math.Ceiling((newValue - previousValue) / (CountFPS * Duration) * 1.0d)); 
            }
            if (stepAmount == 0)
            {
                yield return newValue;

            }
            else {
                if (previousValue < newValue)
                {
                    while (previousValue < newValue)
                    {
                        previousValue += stepAmount;
                        if (previousValue > newValue)
                        {
                            previousValue = newValue;
                        }

                        yield return previousValue;


                        Thread.Sleep(speed);
                    }
                }
                else
                {
                    while (previousValue > newValue)
                    {
                        previousValue += stepAmount;
                        if (previousValue < newValue)
                        {
                            previousValue = newValue;
                        }

                        yield return previousValue;


                        Thread.Sleep(speed);

                    }
                }
            }
           
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                foreach (int item in CountText(Convert.ToInt32(txtNewNumber.Text), Convert.ToInt32(txtSpeed.Text)))
                {
                    this.BeginInvoke(new Action(() => {
                        lblNumber.Text = item.ToString("N0");

                    }));
                }

            });
        }
    }
}

Các bạn có thể chỉnh sửa tốc độ theo ý muốn.

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Number Effect Counter up and down in winform
Đăng bởi: Thảo Meo - Lượt xem: 4626 07:40:07, 12/12/2022DEVEXPRESS   In bài viết

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

Đọc tiếp
.