- [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
[DEVEXPRESS] Hướng dẫn sử dụng BestSizeEstimator Class để Auto Resize XRlabel trong Report
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục giới thiệu đến các bạn các sử dụng class Best
Best
Trong thiết kế Report, nhiều lúc bạn thiết kế giao diện, ví dụ: như tem QR sản xuất của mình ở hình bên dưới.
Ở hình trên các bạn thấy, kích thước của tem nào là cố định 100mm x 62, khi lúc mình thiết kế thì cột tên Nguyên Liệu, nó chỉ có một dòng.
Nhưng khi mà dữ liệu nó dài hơn, thì nó tự động xuống dòng, và các bạn nhìn thấy layout của mình đã bị bể.
Các chữ (for factory only) đã rớt ra khỏi border của khung.
Ở dòng Nguyên liệu trên mình đang sử dụng là font chữ Arial và kích thước là 11px.
Nhưng mình mong muốn ở đây là khi mà dữ liệu của dòng nguyên liệu nó dài lên, thì thằng XR label này sẽ tự động giảm đi kích thước font chữ.
Nhằm đảm bảo cho cột nguyên liệu của chúng ta luôn một dòng và không bị rớt layout.
Dưới đây là kết của mình khi sử dụng Best
Ở hình trên, các bạn đã thấy cột nguyên liệu tự động thu nhỏ kích thước vừa phù hợp cho tem của chúng ta.
Ở thực hiện, các bạn viết sự kiện BeforePrint cho Xrlabel này.
Source code C# AutoResize XRlabel Devexpress:
using DevExpress.XtraReports.UI;
private void xrLabel1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
XRLabel myLabel = sender as XRLabel;
Font myFont = BestSizeEstimator.GetFontToFitBounds(myLabel, "Product: " + myLabel.Text);
if (myFont.Size > 12) {
myLabel.Text = "Product: " + myLabel.Text;
myLabel.Font = myFont;
}
else myLabel.Font = BestSizeEstimator.GetFontToFitBounds(myLabel);
}
Hoặc nếu các bạn nào dùng Devexpress phiên bản dưới 18 thì có thể dùng 2 hàm sau để auto resize.
Source code AutoResize Devexpress < phiên bản 18.
public static void AutoscaleControlText(XRControl control)
{
float controlWidth = control.SizeF.Width;
while (MeasureTextWidthPixels(((XtraReport)control.Report.Report).ReportUnit, control.Text, control.Font) > controlWidth)
control.Font = new Font(control.Font.FontFamily, control.Font.Size - 0.1f, control.Font.Style);
}
public static float MeasureTextWidthPixels(ReportUnit unit, string text, Font font)
{
Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
int factor;
if (unit == ReportUnit.HundredthsOfAnInch)
{
gr.PageUnit = GraphicsUnit.Inch;
factor = 100;
}
else
{
gr.PageUnit = GraphicsUnit.Millimeter;
factor = 10;
}
SizeF size = gr.MeasureString(text, font);
gr.Dispose();
float s = size.Width * factor;
return s;
}
Chúc các bạn thành công!