NEWS

[DEVEXPRESS] Hướng dẫn cách tạo progressbar overlay Gridview C#

[DEVEXPRESS] Hướng dẫn cách tạo progressbar overlay Gridview C#
Đăng bởi: Thảo Meo - Lượt xem: 7169 16:21:53, 10/06/2019DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách tạo Progressbar Overlay Devexpress bằng ngôn ngữ lập trình C#.

[DEVEXPRESS] How to use Progressbar Devexpress Overlay Gridview

Cũng giống mấy bài viết trước, mình đã có hướng dẫn cách tạo overlay để khóa một khối vùng block  đang xử lý và không cho người dùng thao tác vào.

Dưới đây là giao diện demo ứng dụng sử dụng Progress bar overlay C#. Trông pro hơn.

progressbar_overlay_demo_csharp

 

Đầu tiên, các bạn tạo cho mình một class TaskHelper.cs với nội dung như sau:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MainForm
{
    class TaskHelper : IDisposable
    {
       
        private CancellationTokenSource cancellationTokenSource;
        private CancellationToken cancellationToken;

        public TaskHelper()
        {
            this.cancellationTokenSource = new CancellationTokenSource();
            this.cancellationToken = cancellationTokenSource.Token;
        }

     
        public Task<string> Start(IProgress<int> progress)
        {
            return StartInternal(progress);
        }

        public void Cancel()
        {
            this.cancellationTokenSource.Cancel();
        }
      
        public void Dispose()
        {
            Dispose(true);
        }
     
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.cancellationTokenSource != null)
                {
                    this.cancellationTokenSource.Dispose();
                }
            }

            this.cancellationTokenSource = null;
        }    
        private string Calculate(IProgress<int> progress)
        {
            for (int i = 0; i <= 100; i++)
            {
                Thread.Sleep(100);

                this.cancellationToken.ThrowIfCancellationRequested();

                if (progress != null)
                {
                    progress.Report(i);
                }
            }

            return "Đã hoàn tất!";
        }
    
        private Task<string> StartInternal(IProgress<int> progress)
        {
            return Task.Run(() => Calculate(progress), this.cancellationToken);
        }

    }
}

 

Tiếp theo, sẽ tạo tiếp một class ImageHelper.cs C#, dùng để lấy hình ảnh, trong bài viết này mình có sử dụng hai hình ảnh SVG dùng làm cho nút Cancel.

using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.Utils.Svg;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MainForm
{
    public static class ImageHelper
    {        
        public static Image CreateImage(byte[] imageByteArray, ISkinProvider skinProvider = null)
        {
            SvgBitmap svgBitmap = new SvgBitmap(imageByteArray);

            return svgBitmap.Render
            (
                SvgPaletteHelper.GetSvgPalette(skinProvider ?? UserLookAndFeel.Default, ObjectState.Normal),
                ScaleUtils.GetScaleFactor().Height
            );
        }
      
    }
}

 

Và cuối cùng là source code overlay progressbar C# cho form chính form1.cs, khi chúng ta click vào button.

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraSplashScreen;
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 MainForm
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {

        private Image cancelButtonNormalImage;
        private Image cancelButtonActiveImage;
        private OverlayTextPainter overlayTextPainter;
        private OverlayImagePainter overlayImagePainter;
        private TaskHelper taskHelper;

        public Form1()
        {
            InitializeComponent();
            this.cancelButtonNormalImage = GetCancelButtonNormalImage();
            this.cancelButtonActiveImage = GetCancelButtonActiveImage();

            this.overlayImagePainter = new OverlayImagePainter(this.cancelButtonNormalImage, this.cancelButtonActiveImage, ProcessCancelButtonClick);
            this.overlayTextPainter = new OverlayTextPainter();


        }



        private async void btn_start_Click(object sender, EventArgs e)
        {


            string taskResult;

            IOverlaySplashScreenHandle overlayHandle = SplashScreenManager.ShowOverlayForm
            (
                gridControl1,
                customPainter: new OverlayWindowCompositePainter(this.overlayTextPainter, this.overlayImagePainter)
            );

            try
            {
                taskResult = await RunTask();
            }
            finally
            {
                SplashScreenManager.CloseOverlayForm(overlayHandle);


            }

            XtraMessageBox.Show(this, taskResult, "Hoàn tất!");
        }




        private Image GetCancelButtonNormalImage()
        {
            return ImageHelper.CreateImage(Properties.Resources.CancelNormal);
        }

        private Image GetCancelButtonActiveImage()
        {
            return ImageHelper.CreateImage(Properties.Resources.CancelActive);
        }


        private void ProcessCancelButtonClick()
        {
            if (this.taskHelper != null)
            {
                this.taskHelper.Cancel();
            }
        }

        private void ProcessProgressChanged(int value)
        {
            this.overlayTextPainter.Text = value.ToString() + "%";
        }

        private async Task<string> RunTask()
        {
            string taskResult;

            this.taskHelper = new TaskHelper();

            try
            {
                taskResult = await this.taskHelper.Start(new Progress<int>(ProcessProgressChanged));
            }
            catch (OperationCanceledException)
            {
                taskResult = "Đã dừng.";
            }
            finally
            {
                this.taskHelper.Dispose();

                this.taskHelper = null;
            }

            return taskResult;
        }

    }
}

 

Hy vọng bài viết sẽ giúp ích được cho các bạn.

Thanks for watching!

 

DOWNLOAD SOURCE

 

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn cách tạo progressbar overlay Gridview C#
Đăng bởi: Thảo Meo - Lượt xem: 7169 16:21:53, 10/06/2019DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.