C#通过Devexpress控件实现进度条功能

本文介绍如何重写Devexpress默认的等待提示框,提供更友好的用户体验。通过自定义对话框类,实现带有进度条的等待框,并展示具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、Devexpress默认的等待提示框

效果如下:

主要代码如下:

WaitDialogForm wdf = new WaitDialogForm("提示", "正在加载配置......");
     int i = 100;
     for (int j = 1; j < i; j++)
     {
         Thread.Sleep(3000);
         string id = i.ToString() + "%";
         string jd = j.ToString() + "%";
         wdf.SetCaption("执行进度(" +jd + "/" +id+ ")");
     }
     wdf.Close();

如果提示错误,记得引入:

using DevExpress.Utils;

Devexpress默认的等待提示框看起来不够友好,所以我在它的基础上进行了重写。

二、Devexpress升级版含等待条的提示框

效果如下:

是不是感觉高大上好多!

下面上主要代码:

int i = 100;
      ShowDialogForm sdf = new ShowDialogForm("Point", "Loading...", "Please be patient for dozens of seconds ", i);
      for (int j = 1; j < i; j++)
      {
          Thread.Sleep(300);
          string id = j.ToString() + "%";
          string jd = i.ToString() + "%";
          sdf.SetCaption("Execution Progress(" + id + "/" + jd + ")");
      }
      sdf.Close();

注意引入:

using ET.ManagerApp;

主要文件如下

1.ShowDialogForm.cs文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ET.ManagerApp
{
public partial class ShowDialogForm : DevExpress.XtraEditors.XtraForm
{
    #region Fields & Properties
    /// <summary>
    /// 标题
    /// </summary>
    private string caption;

    public string Caption
    {
        get { return caption; }
        set { caption = value; }
    }
    /// <summary>
    /// 消息
    /// </summary>
    private string message;

    public string Message
    {
        get { return message; }
        set { message = value; }
    }
    /// <summary>
    /// 描述
    /// </summary>
    private string content;

    public string Content
    {
        get { return content; }
        set { content = value; }
    }
    /// <summary>
    /// 进度条最小值
    /// </summary>
    private int minProcess = 1;

    public int MinProcess
    {
        get { return minProcess; }
        set { minProcess = value; }
    }
    /// <summary>
    /// 进度条最大值
    /// </summary>
    private int maxProcess = 100;

    public int MaxProcess
    {
        get { return maxProcess; }
        set { maxProcess = value; }
    }
    #endregion

    #region Constructed Function
    public ShowDialogForm()
    {
        InitializeComponent();
    }
    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="_caption">提示</param>
    public ShowDialogForm(string _caption)
        : this(_caption, "", "", 100)
    {
    }

    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="_caption"></param>
    /// <param name="_message"></param>
    public ShowDialogForm(string _caption,string _message) 
        : this(_caption, _message, "",100)
    {
    }

    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="_caption"></param>
    /// <param name="_message"></param>
    /// <param name="_content"></param>
    public ShowDialogForm(string _caption, string _message,string _content)
        : this(_caption, _message, _content, 100)
    {
    }

    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="_caption">提示</param>
    /// <param name="_message">消息内容</param>
    /// <param name="_content">详细描述</param>
    /// <param name="_maxProcess">进度条最大值</param>
    public ShowDialogForm(string _caption, string _message,string _content,int _maxProcess)
        : this()
    {
        this.Caption = "";
        this.Message = "";
        this.Content = "";

        this.Caption = _caption == "" ? "提示" : _caption;
        this.Message = _message == "" ? "正在加载,请稍后......" : _message;
        this.Content = _content;
        this.maxProcess = _maxProcess > this.MinProcess ? _maxProcess : MinProcess;

        lblCaption.Text = this.Caption;
        lblMessage.Text = this.Message;
        lblContent.Text = this.Content;
        progressShow.Properties.Minimum = MinProcess;
        progressShow.Properties.Maximum = MaxProcess;
        progressShow.Properties.Step = 1;
        progressShow.PerformStep();

        this.ShowInTaskbar = false;
        this.TopMost = true;
        this.Show();
        this.Refresh();
    }
    #endregion

    #region Methods
    /// <summary>
    /// 设置提示
    /// </summary>
    /// <param name="newCaption"></param>
    public void SetCaption(string newCaption)
    {
        this.Caption = newCaption;
        lblCaption.Text = this.Caption;
        progressShow.PerformStep();
        this.Refresh();
    }
    /// <summary>
    /// 设置消息
    /// </summary>
    /// <param name="newMessage"></param>
    public void SetMessage(string newMessage)
    {
        this.Message = newMessage;
        lblMessage.Text = this.Message;
        progressShow.PerformStep();
        this.Refresh();
    }
    /// <summary>
    /// 设置描述
    /// </summary>
    /// <param name="newContent"></param>
    public void SetContent(string newContent)
    {
        this.Content = newContent;
        lblContent.Text = this.Content;
        progressShow.PerformStep();
        this.Refresh();
    }
    #endregion

    #region Events
    protected override void OnClosing(CancelEventArgs e)
    {
        base.OnClosing(e);
    }
    #endregion
}
}

2.ShowDialogForm.Designer.cs文件

namespace ET.ManagerApp
{
partial class ShowDialogForm
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.panelControl1 = new DevExpress.XtraEditors.PanelControl();
        this.lblCaption = new DevExpress.XtraEditors.LabelControl();
        this.lblMessage = new DevExpress.XtraEditors.LabelControl();
        this.lblContent = new DevExpress.XtraEditors.LabelControl();
        this.progressShow = new DevExpress.XtraEditors.ProgressBarControl();
        this.panelControl2 = new DevExpress.XtraEditors.PanelControl();
        ((System.ComponentModel.ISupportInitialize)(this.panelControl1)).BeginInit();
        this.panelControl1.SuspendLayout();
        ((System.ComponentModel.ISupportInitialize)(this.progressShow.Properties)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.panelControl2)).BeginInit();
        this.panelControl2.SuspendLayout();
        this.SuspendLayout();
        // 
        // panelControl1
        // 
        this.panelControl1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Style3D;
        this.panelControl1.Controls.Add(this.lblCaption);
        this.panelControl1.Dock = System.Windows.Forms.DockStyle.Top;
        this.panelControl1.Location = new System.Drawing.Point(0, 0);
        this.panelControl1.Name = "panelControl1";
        this.panelControl1.Size = new System.Drawing.Size(436, 34);
        this.panelControl1.TabIndex = 0;
        // 
        // lblCaption
        // 
        this.lblCaption.Location = new System.Drawing.Point(5, 9);
        this.lblCaption.Name = "lblCaption";
        this.lblCaption.Size = new System.Drawing.Size(41, 14);
        this.lblCaption.TabIndex = 0;
        this.lblCaption.Text = "Caption";
        // 
        // lblMessage
        // 
        this.lblMessage.Location = new System.Drawing.Point(24, 7);
        this.lblMessage.Name = "lblMessage";
        this.lblMessage.Size = new System.Drawing.Size(46, 14);
        this.lblMessage.TabIndex = 2;
        this.lblMessage.Text = "Message";
        // 
        // lblContent
        // 
        this.lblContent.Location = new System.Drawing.Point(24, 31);
        this.lblContent.Name = "lblContent";
        this.lblContent.Size = new System.Drawing.Size(45, 14);
        this.lblContent.TabIndex = 3;
        this.lblContent.Text = "Content";
        // 
        // progressShow
        // 
        this.progressShow.EditValue = 1;
        this.progressShow.Location = new System.Drawing.Point(24, 59);
        this.progressShow.Name = "progressShow";
        this.progressShow.Properties.Appearance.BackColor = System.Drawing.Color.Transparent;
        this.progressShow.Properties.Appearance.ForeColor = System.Drawing.Color.Black;
        this.progressShow.Properties.EndColor = System.Drawing.Color.Empty;
        this.progressShow.Properties.LookAndFeel.SkinName = "Blue";
        this.progressShow.Properties.LookAndFeel.UseDefaultLookAndFeel = false;
        this.progressShow.Properties.LookAndFeel.UseWindowsXPTheme = true;
        this.progressShow.Properties.ReadOnly = true;
        this.progressShow.Properties.ShowTitle = true;
        this.progressShow.Properties.StartColor = System.Drawing.Color.Empty;
        this.progressShow.Properties.Step = 1;
        this.progressShow.Size = new System.Drawing.Size(400, 15);
        this.progressShow.TabIndex = 4;
        // 
        // panelControl2
        // 
        this.panelControl2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.panelControl2.Controls.Add(this.lblContent);
        this.panelControl2.Controls.Add(this.progressShow);
        this.panelControl2.Controls.Add(this.lblMessage);
        this.panelControl2.Location = new System.Drawing.Point(0, 38);
        this.panelControl2.Name = "panelControl2";
        this.panelControl2.Size = new System.Drawing.Size(436, 83);
        this.panelControl2.TabIndex = 5;
        // 
        // ShowDialogForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(436, 124);
        this.Controls.Add(this.panelControl2);
        this.Controls.Add(this.panelControl1);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "ShowDialogForm";
        this.ShowIcon = false;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "ShowDialogForm";
        ((System.ComponentModel.ISupportInitialize)(this.panelControl1)).EndInit();
        this.panelControl1.ResumeLayout(false);
        this.panelControl1.PerformLayout();
        ((System.ComponentModel.ISupportInitialize)(this.progressShow.Properties)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.panelControl2)).EndInit();
        this.panelControl2.ResumeLayout(false);
        this.panelControl2.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private DevExpress.XtraEditors.PanelControl panelControl1;
    private DevExpress.XtraEditors.LabelControl lblCaption;
    private DevExpress.XtraEditors.LabelControl lblMessage;
    private DevExpress.XtraEditors.LabelControl lblContent;
    private DevExpress.XtraEditors.ProgressBarControl progressShow;
    private DevExpress.XtraEditors.PanelControl panelControl2;
}
}

3.ShowDialogForm.resx文件

<?xml version="1.0" encoding="utf-8"?>
<root>
  <!-- 
Microsoft ResX Schema 

Version 2.0

The primary goals of this format is to allow a simple XML format 
that is mostly human readable. The generation and parsing of the 
various data types are done through the TypeConverter classes 
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
    <value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
    <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
    <comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple 
name/value pairs.

Each data row contains a name, and value. The row also contains a 
type or mimetype. Type corresponds to a .NET class that support 
text/value conversion through the TypeConverter architecture. 
Classes that don't support this are serialized and stored with the 
mimetype set.

The mimetype is used for serialized objects, and tells the 
ResXResourceReader how to depersist the object. This is currently not 
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format 
that the ResXResourceWriter will generate, however the reader can 
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value   : The object must be serialized with 
        : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        : and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value   : The object must be serialized with 
        : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
        : and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value   : The object must be serialized into a byte array 
        : using a System.ComponentModel.TypeConverter
        : and then encoded with base64 encoding.
-->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
  <xsd:complexType>
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="metadata">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="value" type="xsd:string" minOccurs="0" />
          </xsd:sequence>
          <xsd:attribute name="name" use="required" type="xsd:string" />
          <xsd:attribute name="type" type="xsd:string" />
          <xsd:attribute name="mimetype" type="xsd:string" />
          <xsd:attribute ref="xml:space" />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="assembly">
        <xsd:complexType>
          <xsd:attribute name="alias" type="xsd:string" />
          <xsd:attribute name="name" type="xsd:string" />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="data">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
            <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
          </xsd:sequence>
          <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
          <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
          <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
          <xsd:attribute ref="xml:space" />
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="resheader">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
          </xsd:sequence>
          <xsd:attribute name="name" type="xsd:string" use="required" />
        </xsd:complexType>
      </xsd:element>
    </xsd:choice>
  </xsd:complexType>
</xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
<value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
<value>2.0</value>
  </resheader>
  <resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
</root>

更多博客内容详见我的博客 Wang's Blog


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坏菠萝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值