记录个简单的进度条同步显示方法

//进度条同步显示的方法
public
void CommonProgressHandle(Action<Action> bizAct, int max, string msg) { using (SimpleProgress sp = new SimpleProgress()) { sp.Message = msg; sp.Position = 0; sp.Max = max; sp.Show(); Action proNextStep = new Action(() => { sp.Position++; sp.Message = msg + string.Format("({0}/{1})", sp.Position, sp.Max); }); if (bizAct != null) { bizAct(proNextStep); } } }

simpleProgress是个窗体

public partial class SimpleProgress : XtraForm
{
    public SimpleProgress()
    {
        InitializeComponent();
        

    }

    private string _Message = string.Empty;
    public string Message
    {
        get
        {
            return _Message;
        }
        set
        {
            _Message = value;
            ProgressChanged();
        }
    }

    public int Max
    {
        get
        {
            return _Max;
        }
        set
        {
            _Max = value;
            ProgressChanged();
        }
    }
    private int _Max;

    public int Position
    {
        get
        {
            return _Position;
        }

        set
        {
            _Position = value;
            ProgressChanged();
        }
    }
    private int _Position;

    private void ProgressChanged()
    {
        if (this.IsHandleCreated)
        {
            this.Invoke((EventHandler)delegate
            {
                this.lblMessage.Text = _Message;
                this.progressBar1.Properties.Maximum = _Max;
                if (_Position >= this.progressBar1.Properties.Minimum && _Position <= this.progressBar1.Properties.Maximum)
                {
                    this.progressBar1.Position = _Position;
                    this.progressBar1.Refresh();
                }
                this.Refresh();
            });
        }
    }

    //public void ShowAtParent(SimpleProgress sp, Form owner)
    //{
    //    if (sp == null)
    //    {
    //        return;
    //    }
    //    if (owner == null)
    //    {

    //    } 
    //    sp.TopMost = false;
    //    sp.TopLevel = false;
    //    owner.Controls.Add(sp);
    //    Point centerPoint = new Point((Screen.PrimaryScreen.WorkingArea.Width - sp.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - sp.Height) / 2);
    //    Point clientCenterPoint = PointToClient(centerPoint);
    //    sp.Left = clientCenterPoint.X;
    //    sp.Top = clientCenterPoint.Y;
    //    sp.BringToFront();
    //    sp.Show();
    //}
}

 

 

调用的地方:

CommonProgressHandle(nextStep =>
{
    nextStep();
},
count,
"正在导入XX数据,请稍后......");

 

posted @ 2024-04-24 09:26  卡萨丁·周  阅读(38)  评论(0)    收藏  举报