C# - 进度条不会显示值

时间:2015-11-09 17:55:54

标签: c# progress-bar

在我的应用程序中,我需要一个进度条来显示植物生长的进度。这将是代码:

    private static Timer farmProgress;
    internal void initFarmProgTimer( int step, int max = 100 )
    {
        farmProgress = new Timer();
        farmProgress.Tick += new EventHandler(farmProgress_Tick);
        farmProgress.Interval = step; // in miliseconds
        farmProgress.Start();



    }
    private void farmProgress_Tick(object sender, EventArgs e)
    {
        if (increment >= 100)
        {
            // wait till user get plant
        }
        else
        {
            increment++;
            plantProgressBar.Value = increment;
        }
    }

这里是initFarmProgTimer函数的调用:

    public static System.Threading.Timer growTimer;
    public static void InitGrowTimer(int time, string name)
    {
        growTimer = new System.Threading.Timer(growTimer_Finished, null, time, Timeout.Infinite);
        plantActive = true;

        Menu menu = new Menu();
        menu.initFarmProgTimer(time / 100);
    }

请注意,调用此函数的类不是表单,而是定义函数的类是表单。

有人知道我的错误是什么吗?

修改 这是对InitGrowTimer函数的调用

    switch ( index )
        {
            case 0:
                currentPlant = wheat.name;
                plantQ = printPlantDatas("wheat");
                if (plantQ == true)
                {
                    InitGrowTimer(wheat.time, wheat.name);
                    wheat.planted++;
                }
                break;
        }

2 个答案:

答案 0 :(得分:0)

您正在将值设置为进度条,该进度条仅将进度设置为当前值。你必须增加它。我为你添加了(+)符号

 private void farmProgress_Tick(object sender, EventArgs e)
    {
        if (increment >= 100)
        {
            // wait till user get plant
        }
        else
        {
            increment++;
            plantProgressBar.Value += increment;
        }
    }

答案 1 :(得分:0)

我不清楚表格中的内容是什么,什么不是,但假设这是在表格上:

    private void farmProgress_Tick(object sender, EventArgs e)
    {
        if (increment >= 100)
        {
            // wait till user get plant
        }
        else
        {
            increment++;                
            this.Invoke(new Action(() => {
                       plantProgressBar.Value = increment;
               }));
        }
    }

将其更改为此,以便从UI线程更新控件:

public partial class Form1 : Form
{
    private Timer farmProgress;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        farmProgress = new Timer();
        farmProgress.Tick += farmProgress_Tick;
        farmProgress.Interval = 1000; // in miliseconds
        farmProgress.Start();
    }

    void farmProgress_Tick(object sender, EventArgs e)
    {
        progressBar1.Value++;
    }
}
  

更新

我的回答是错误的,我没想到这一点,但我创建了一个Forms应用程序,这可以很好地增加进度条:

mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 5000, new GoogleMap.CancelableCallback() {
        @Override
        public void onFinish() {
            mMap.animateCamera(CameraUpdateFactory.zoomTo(16), 3000, myCallBack);
        }

        @Override
        public void onCancel() {

        }
    });
}

GoogleMap.CancelableCallback myCallBack = new GoogleMap.CancelableCallback() {
@Override
public void onFinish() {
    if(++COUNT < hashMarkers.size()){

        CameraPosition cameraPosition =
                new CameraPosition.Builder()
                        .target(hashMarkers.get(String.valueOf(COUNT)).getPosition())
                        .tilt(90)
                        .bearing(-bearing)
                        .zoom(16)
                        .build();

        hashMarkers.get(String.valueOf(COUNT)).setVisible(true);

        mMap.animateCamera(
                CameraUpdateFactory.newCameraPosition(cameraPosition),
                3000,
                COUNT == hashMarkers.size() - 1 ? FinalCancelableCallback : myCallBack);
    }
}

@Override
public void onCancel() {
}
};