- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
[C#] Number Effect Counter up and down in winform
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:
Ở 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!