当前位置:编程学习 > C#/ASP.NET >>

写了一个进度条的程序,直接显示结果,怎么显示递增的过程呢


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void showPro(int value)
        {
            this.progressBar1.Value = value;
            this.label1.Content = value + "%";
            
            System.Threading.Thread.Sleep(1000);//停止一秒后继续+

        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {

            int xueTiao = 0;
            while (xueTiao < 100)
            {

                xueTiao++;
                showPro(xueTiao);
                
            }
            MessageBox.Show("满了");

        }

    }
}
进度条 --------------------编程问答-------------------- 不建议这样弄,这样弄即使有递增过程也是假的,建议你的 xueTiao++;放到实际业务处理完成后 --------------------编程问答-------------------- 递增做成一个方法,递增多少有具体业务来给值。最后一个业务给值100% --------------------编程问答--------------------

while (true)
            {
                //System.Threading.Thread.Sleep(500); 此处可以加这句
                progressBar1.Value += 1;
                if (progressBar1.Value>=progressBar1.Maximum)
                {
                    return;
                }
            }
--------------------编程问答-------------------- 界面要刷新吧,在

private void showPro(int value)         
{             
this.progressBar1.Value = value;             
this.label1.Content = value + "%";    
Application.DoEvents();//加这条,将消息给处理显示到界面上

System.Threading.Thread.Sleep(1000);//停止一秒后继续+           


--------------------编程问答--------------------
引用 4 楼 much0726 的回复:
界面要刷新吧,在

private void showPro(int value)         
{             
this.progressBar1.Value = value;             
this.label1.Content = value + "%";    
Application.DoEvents();//加这条,将消息给处理显示到界面上

System.Threading.Thread.Sleep(1000);//停止一秒后继续+           



错误1“System.Windows.Application”并不包含“DoEvents”的定义

报错啊!没有这个方法貌似
--------------------编程问答--------------------
引用 3 楼 diaodiaop 的回复:

while (true)
            {
                //System.Threading.Thread.Sleep(500); 此处可以加这句
                progressBar1.Value += 1;
                if (progressBar1.Value>=progressBar1.Maximum)
                {
                    return;
                }
            }

加了  没管用啊 ! --------------------编程问答-------------------- 凡是后台运行程序,前台做显示的都要用到线程。委托 事件 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 我的是WPF --------------------编程问答-------------------- 通过引用的方式进行传值 后台的数据处理过程中直接就将界面中的进度条值进行修改了。 
--------------------编程问答-------------------- 你是

引用 5 楼 xiaoshi657 的回复:
Quote: 引用 4 楼 much0726 的回复:

界面要刷新吧,在

private void showPro(int value)         
{             
this.progressBar1.Value = value;             
this.label1.Content = value + "%";    
Application.DoEvents();//加这条,将消息给处理显示到界面上

System.Threading.Thread.Sleep(1000);//停止一秒后继续+           



错误1“System.Windows.Application”并不包含“DoEvents”的定义

报错啊!没有这个方法貌似

可能少了引用吧,百度下Application.DoEvents
顺便说下,这个方法界面还是会卡,毕竟是单线程的,用委托的方法界面会流畅些。 --------------------编程问答--------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void showPro(int value)
        {

            this.Invoke((EventHandler)delegate
            {
                this.progressBar1.Value = value;
                this.label1.Content = value + "%";
            });

            System.Threading.Thread.Sleep(1000);//停止一秒后继续+

        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Thread th = new Thread(new ThreadStart(ProgressBarAdd));
            th.Start();

        }

        private void ProgressBarAdd()
        {
            int xueTiao = 0;
            while (xueTiao < 100)
            {

                xueTiao++;
                showPro(xueTiao);

            }
            MessageBox.Show("满了");
        }

    }
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,