- [C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
- [VB.NET] Chia sẻ source tạo sắp xếp đội hình bóng đá Line-ups đội bóng
- [C#] Hướng dẫn chỉnh sửa Text của label trực tiếp trên winform
- [C#] Hướng dẫn custom TextBox giống Ultraviewer trên Winform
- [C#] Show Modal Winform like Bootstrap
- [DATABASE] Thứ tự thực hiện mệnh đề truy vấn SELECT trong Sqlserver
- [C#] Hướng dẫn viết addin Excel Lấy hình ảnh từ URL internet vào Excel
- [DATABASE] TSQL view max length all column data trên table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng MailMerge kèm Hình ảnh trên Winform
- [DATABASE] Hướng dẫn truy vấn xem kích thước lưu trữ của từng bảng ghi Table trên sqlserver
- [C#] Hướng dẫn Fake Date Time sử dụng thư viện Harmony
- [DATABASE] Phân biệt câu lệnh DDL và DML trong sqlserver
- [C#] Hướng dẫn convert file mã HTML sang file Pdf trên winform
- [DEVEXPRESS] Tạo các loại mã vạch Barcode trực tiếp trên Devexpress Barcode API
- [DEVEXPRESS] Hướng dẫn custom Simple button thành Progressbar
- [DATABASE] Tách số và chữ từ chuỗi - hàm tối ưu hóa tách số và chữ trong Sqlserver
- [C#] Tìm kiếm gần đúng Full Text Search sử dụng thư viện Lucene.NET
- [C#] Chia sẻ tài liệu, sdk và source code máy chấm công dòng máy ZKTeco
- [C#] Memory Cache là gì, và sử dụng trong ứng dụng Winform
- [DATABASE] Khóa chính Primary Key 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!