- [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
[DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
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 custom Simple Button trên Devexpress thành progress bar trên C#, winform.
[DEVEPXRESS] How to custom Simple Button aLike Progress bar in C# Winform
Dưới đây là hình ảnh giao diện ứng dụng:
Các bạn thấy khi mình click vào button uploading, thì cây progressbar sẽ hiển thị trực tiếp trên Simple Button, thay vì chúng ta phải sử dụng thêm component Progressbar.
Video Demo ứng dụng C#:
Đầu tiên các bạn tạo một class UploadButton.cs
:
using DevExpress.Utils.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SimpleButtonProgressBar
{
public partial class UploadButton : DevExpress.XtraEditors.SimpleButton
{
public UploadButton()
{
InitializeComponent();
}
public UploadButton(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public int Progress { get; set; }
public Color ColorProgress { get; set; } = Color.Green;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw the progress bar
using (var brush = new SolidBrush(Color.FromArgb(128, ColorProgress)))
{
var brush2 = new SolidBrush(Color.FromArgb(255, Color.Yellow));
var width = (int)((float)(this.Width +2) * ((float)this.Progress / 100f));
if (this.Progress == 100)
{
e.Graphics.FillRectangle(brush2, 0, 0, 0, this.Height);
brush2.Dispose();
}
else {
e.Graphics.FillRectangle(brush, 0, 0, width, this.Height);
}
}
}
}
}
Tiếp đến, ở Form1.cs, các bạn kéo component Upload button ra sử dụng và code demo show progress bar như code bên dưới:
using DevExpress.XtraEditors.ButtonsPanelControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace SimpleButtonProgressBar
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
Color colorButtonDefault;
public Form1()
{
InitializeComponent();
colorButtonDefault = uploadButton1.BackColor;
colorPickEdit1.Color = uploadButton1.ColorProgress;
}
private void uploadButton1_Click(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += (s, args) =>
{
for (int i = 1; i <= 100; i++)
{
uploadButton1.Progress = i;
uploadButton1.BeginInvoke(new Action(() =>
{
uploadButton1.Refresh();
uploadButton1.Text = "Uploading... " + i.ToString() + "%";
}));
Thread.Sleep(20);
}
uploadButton1.BeginInvoke(new Action(() =>
{
uploadButton1.Text = "Upload file";
uploadButton1.Refresh();
}));
};
worker.RunWorkerAsync();
}
private void pictureEdit1_EditValueChanged(object sender, EventArgs e)
{
}
private void colorPickEdit1_EditValueChanged(object sender, EventArgs e)
{
uploadButton1.ColorProgress = colorPickEdit1.Color;
}
}
}
Thanks for watching!