- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
[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!