- [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
[C#] Hướng dẫn vẽ Progressbar trong Button Winform
Xin chào các bạn, hôm nay mình sẽ hướng dẫn các bạn cách vẽ Progressbar trên Button trong lập trình C# Winform.
[C#] Draw Progressbar in Button Winform
Trong lập trình ứng dụng C# winform, khi các bạn viết các ứng dụng để xử lý.
Ví dụ: Click vào button để thực hiện chức năng download, khi download trả về phần trăm Progress thì các bạn hay sử dụng componet Progressbar của Winform để view.
Trong bài này, mình sẽ tích hợp khi click vào button download thì progress trả về được bao nhiêu phần trăm sẽ vẽ trực tiếp trên Button đó luôn.
Các bạn xem file demo ứng dụng Progressbar in Button C#:
Source code Progressbar winform c#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DrawProgressbarButton
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ColorsBm = new Bitmap(btn_download.ClientSize.Width, btn_download.ClientSize.Height);
btn_download.BackgroundImage = ColorsBm;
}
private Bitmap ColorsBm;
private int ProgressColorBar = -1;
private void timer1_Tick(object sender, EventArgs e)
{
const int max_progress = 100;
ProgressColorBar++;
btn_download.BeginInvoke(new Action(() =>
{
if (ProgressColorBar > 0)
{
btn_download.Text = ProgressColorBar + "%";
}
}));
if (ProgressColorBar >= max_progress)
{
ProgressColorBar = -1;
btn_download.Text = "Download";
using (Graphics gr = Graphics.FromImage(ColorsBm))
{
gr.Clear(btn_download.BackColor);
}
timer1.Enabled = false;
return;
}
using (SolidBrush br = new SolidBrush(Color.FromArgb(50, 40, 169, 67)))
{
using (Graphics gr = Graphics.FromImage(ColorsBm))
{
float wid = ColorsBm.Width * ProgressColorBar / (max_progress - 5);
float hgt = ColorsBm.Height;
RectangleF rect = new RectangleF(0, 0, wid, hgt);
gr.FillRectangle(br, rect);
}
}
btn_download.Refresh();
}
private void btn_download_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
using (Graphics gr = Graphics.FromImage(ColorsBm))
{
gr.Clear(btn_download.BackColor);
}
}
}
}
Thanks for watching!