- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Danh sách tài khoản ChatGPT miễn phí - Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
[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!