C#进度条不显示值

时间:2015-11-09 16:04:42

标签: 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;
        }

修改 添加了完整的Menu.cs和Farm.cs文件。

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

namespace Civilisation
{
partial class Farm
{

    /* START PLANT DEFINITONS

        CONFIGUREABLE! You may edit the values used.

        Definition standards:
        private const string <plant>Name = "<plant>";           constant plant name string
        private const int <plant>EXP = <exp>;                   exp you get for harvesting
        private const int <plant>Time = <seconds> * 1000;       time measured in ms
    */

    public struct wheat
    {
        public const string name = "Wheat";
        public const int exp = 2;
        public const int time = 5 * 1000;
        public static int planted = 0;
    }
    public struct carrot
    {
        public const string name = "Carrot";
        public const int exp = 10;
        public const int time = 30 * 1000;
        public static int planted = 0;
    }
    public struct potatoe
    {
        public const string name = "Potatoe";
        public const int exp = 30;
        public const int time = 180 * 1000;
        public static int planted = 0;
    }

    public struct chili
    {
        public const string name = "Chili";
        public const int exp = 60;
        public const int time = 300 * 1000;
        public static int planted = 0;
    }






    public struct magicbeans
    {
        public const string name = "Magic XP Beans";
        public static int exp = 0;
        public const int time = 0;
        public static int planted = 0;
    }

    // END PLANT DEFINITIONS

    public static List<string> plants;
    public static void InitializeList()
    {
        plants = new List<string>();
        plants.Add("wheat");
        plants.Add("carrot");
        plants.Add("potatoe");
        plants.Add("chili");
        plants.Add("admin.magicxpbeans");
        plants.Add("cotton");
    }

    private static bool plantActive = false;
    private static string currentPlant;

    private static int playerLevel = 1;
    private static int playerEXP = 0;

    public static void levelUp()
    {
        if (playerLevel == 1)
        {
            if (playerEXP >= Program.level1)
            {
                playerLevel++;
                playerEXP -= Program.level1;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level2 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 2)
        {
            if (playerEXP >= Program.level2)
            {
                playerLevel++;
                playerEXP -= Program.level2;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level3 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 3)
        {
            if (playerEXP >= Program.level3)
            {
                playerLevel++;
                playerEXP -= Program.level3;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level4 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 4)
        {
            if (playerEXP >= Program.level4)
            {
                playerLevel++;
                playerEXP -= Program.level4;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level5 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 5)
        {
            if (playerEXP >= Program.level5)
            {
                playerLevel++;
                playerEXP -= Program.level5;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level6 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 6)
        {
            if (playerEXP >= Program.level6)
            {
                playerLevel++;
                playerEXP -= Program.level6;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level7 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 7)
        {
            if (playerEXP >= Program.level7)
            {
                playerLevel++;
                playerEXP -= Program.level7;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level8 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 8)
        {
            if (playerEXP >= Program.level8)
            {
                playerLevel++;
                playerEXP -= Program.level8;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level9 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
        if (playerLevel == 9)
        {
            if (playerEXP >= Program.level9)
            {
                playerLevel++;
                playerEXP -= Program.level9;
                MessageBox.Show("Congratulations! You have reached level " + playerLevel + " in farming! " + (Program.level10 - playerEXP) + "EXP to next level.\n", "Levelup!", MessageBoxButtons.OK);
            }
        }
    }

    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, time);
    }
    public static void plantCrop( int index )
    {
        bool plantQ;

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

    private static int growTime = 0;
    private static int xpGained = 0;
    private static int timesPlanted = 0;
    private static void growTimer_Finished(object sender)
    {
        switch (currentPlant.ToLower())
        {
            case "wheat":
                Inventory.wheat.quant++;
                break;
            case "carrot":
                Inventory.carrot.quant++;
                break;
            case "potatoe":
                Inventory.potatoe.quant++;
                break;
            case "chili":
                Inventory.chili.quant++;
                break;
        }
        plantActive = false;
        MessageBox.Show("Your " + currentPlant + " is finished!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Information);
        currentPlant = "";
        playerEXP += xpGained;
    }

    public static bool printPlantDatas(string plant)
    {
        switch (plant.ToLower())
        {
            case "wheat":
                growTime = wheat.time;
                xpGained = wheat.exp;
                timesPlanted = wheat.planted;
                break;
            case "carrot":
                growTime = carrot.time;
                xpGained = carrot.exp;
                timesPlanted = carrot.planted;
                break;
            case "potatoe":
                growTime = potatoe.time;
                xpGained = potatoe.exp;
                timesPlanted = potatoe.planted;
                break;
            case "chili":
                growTime = chili.time;
                xpGained = chili.exp;
                timesPlanted = chili.planted;
                break;


            case "magicbeans":
                growTime = magicbeans.time;
                xpGained = magicbeans.exp;
                timesPlanted = magicbeans.planted;
                break;
            default:
                MessageBox.Show("FATAL ERROR\nThe plant is contained in the plant menu but not in the data printer!", "Civilisation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
        }
        var result = MessageBox.Show(currentPlant + " needs " + growTime / 1000 + " seconds to grow and gives " + xpGained + "EXP. You have so far harvested it " + timesPlanted + " times.\nDo you want to plant it?", "Plant Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        if ( result == DialogResult.OK )
        {
            return true;
        }
        if ( result == DialogResult.Cancel )
        {
            MessageBox.Show("Planting cancelled.", "Plant Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }
        else
        {
            MessageBox.Show("Planting cancelled.", "Plant Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return false;
        }
    }
}
}

另一个档案:

using Civilisation.Forms;
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 Civilisation
{
public partial class Menu : Form
{
    public Menu()
    {
        InitializeComponent();
        initGoldTimer();
    }

    private void farmButton_Click(object sender, EventArgs e)
    {
        FarmForm farmform = new FarmForm();
        farmform.ShowDialog();
    }

    private void inventoryButton_Click(object sender, EventArgs e)
    {
        InventoryForm invform = new InventoryForm();
        invform.ShowDialog();
    }







    private static Timer loadCurrency;
    private void initGoldTimer ()
    {
        loadCurrency = new Timer();
        loadCurrency.Tick += new EventHandler(loadCurrency_Tick);
        loadCurrency.Interval = 100; // in miliseconds
        loadCurrency.Start();
    }
    private void loadCurrency_Tick(object sender, EventArgs e)
    {
        gold.Text = "Gold: " + Currency.gold;
        gems.Text = "Gems: " + Currency.gems;
    }


    private int increment = 0;

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

        this.plantProgressBar.Maximum = max;

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

        }
    }



    private static Timer mineProgress;
    private void initMineTimer()
    {
        mineProgress = new Timer();
        mineProgress.Tick += new EventHandler(mineProgress_Tick);
        mineProgress.Interval = 100; // in miliseconds
        mineProgress.Start();
    }
    private void mineProgress_Tick(object sender, EventArgs e)
    {

    }

}
}

1 个答案:

答案 0 :(得分:0)

咦?据我所知,我的问题没有得到解答,我在评论中提出的问题是解决问题的一部分。我阅读了这些指南,但我很难过你不明白这个问题。