Rps
2017-03-21 08:06
采纳率: 50%
浏览 933

C#中,(winform)进度条对象的赋值不是引用的赋值吗?

问题的起因在这里:

                //创建的临时进度条对象。
                ProgressBar temp = new ProgressBar();
                //@progressBar1 是在窗体中创建进度条时,自动给的对象。
                temp = progressBar1;
                //让@temp的进度条走到89的位置,但是并没有什么卵用。
                temp.Value = 89;

请问,如果我想用赋值的方式,让两个对象都能够控制进度条,我该怎么去写。

啊,突然想到了“委托”。这个有用吗?该怎么写?
初学C#的小白,求助中……

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Rps 2017-03-21 08:24
    已采纳

    我的源代码在这里,虽然太长没人看。

    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Threading;/*线程相关*/
    using System.Windows.Forms;/*窗体相关*/
    using System.Timers;/*计时器相关*/
    
    namespace myWinform1
    {
        class play:Form1
        {
            private const int playerNums = 3;
            private ParameterizedThreadStart[] games;
            private Thread[] gameReady = new Thread[playerNums];
            private ProgressBar[] track = new ProgressBar[playerNums];
            private Player[] playerArray = new Player[playerNums];
            private System.Timers.Timer t = new System.Timers.Timer();
            private static List<int> greads = new List<int>();
    
            public System.Timers.Timer Tim
            {
                get { return t; }
            }
            public Thread[] GameReady
            {
                get { return gameReady; }
            }
            public int PlayerNums
            {
                get{ return playerNums; }
            }
            public Player[] PlayerArray
            {
                get { return playerArray; }
            }
            public play(/* int paraPlayerNums = 3 */)
            {
                /*设置计时器*/
                t.Interval = 1000;/*间隔*/
                t.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Func);/*到达时间应该执行的事件*/
                t.Enabled = true; /*允许执行事件*/
                t.AutoReset = true; /*不仅仅执行1次,而是始终执行。*/
    
                ///*设置比赛人数。默认3人*/
                //this.playerNums = paraPlayerNums;
                /*创建比赛进程*/
                this.games = new ParameterizedThreadStart[] { null,null,null };
                /*创建赛道*/
                this.track = new ProgressBar[] { progressBar1, progressBar2, progressBar3 };
                /*分配*/
                for (int i = 0; i < playerNums; i++)
                {
                    /*每个赛道一个进程*/
                    games[i] = new ParameterizedThreadStart(gameStart_threadFunc);
                    /*设置队员属性*/
                    playerArray[i] = new Player(i.ToString() , i , i+6);
                    /*设置进程*/
                    gameReady[i] = new Thread(games[i]);
                    /*为队员分配赛道*/
                    playerArray[i].Track = track[i]; // fixme :这里应该用引用赋值。还是本身就是引用赋值?为什么传递不过去?(C#中如何做到引用赋值?)
                }
            }
    
            ~play()
            { 
                //todo: 销毁函数。包括线程,被创建对象等等。
    
            }
            /*进程函数(比赛进行中)*/
            static bool isTimeReach = true;
            public static void gameStart_threadFunc(object obj)
            {
                /*处理传入参数*/
                Player p = obj as Player;
                while (!p.IsWiner)
                {
                    if (isTimeReach)
                    {
                        int step = p.OneStep;
                        /*处理进度条可能溢出情况*/
                        if (step > p.Track.Maximum - p.Track.Value) 
                        {
                            p.Track.Value = p.Track.Maximum;
                            step = 0;
                            p.IsWiner = true;
                            if (-1 == greads.IndexOf(p.ID)) // fixme :there is a bug.
                            {
                                greads.Add(p.ID);
                                p.showGread();
                            }
                        }
                        /*开始移动*/
                        if (step < 0 && p.Track.Value < (-1*step))
                        {
                            step = 0;
                        }
                        else
                        {
                            p.Track.Value += step;
                        }
                        isTimeReach = false;
                    }
                }
            }
            private void Timer_Func(object sender, System.Timers.ElapsedEventArgs e)
            {
                isTimeReach = true;
                //Invalidate(false);
            }  
    
        }
    }
    
    

    sadasdasdasdsadasd

     using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace myWinform1
    {
        class Player : Form1
        {
            private string name = "";
            private int id = -1;
            private bool isWiner = false;
            private int gread = -1;
            private int attackPotential = 0;
            private int distance = 0;
            private ProgressBar track = new ProgressBar();
            private int oneStep = 0;
            private int baseStep = 6;
            private Random ra = new Random();
            public int Gread 
            {
                get { return gread; }
                set { Gread = gread; }
            }
            public bool IsWiner
            {
                get { return isWiner; }
                set { isWiner = IsWiner; }
            }
            public int AttackPotential
            {
                get { return attackPotential; }
                set { attackPotential = AttackPotential; }
            }
            public int ID
            {
                get { return id; }
            }
            public int Distance
            {
                get { return distance; }
                set { distance = Distance; }
            }
            public ProgressBar Track
            {
                get { return track; }
                set { track = Track; }
            }
            public int OneStep
            {
                get { return oneStep; }
                set { oneStep = OneStep; }
            }
            public Player()
            {
                name = "";
                isWiner = false;
                id = 0xff;
                gread = 0;
                attackPotential = 0; /*潜力值*/
                oneStep = ra.Next(baseStep - attackPotential , attackPotential);/*队员的每步移动距离。它是随机的。*/
            }
            public Player(string paraName, int paraID,int paraAttackPotential)
            {
                isWiner = false;
                gread = 0;
    
                name = paraName;
                id = paraID;
                attackPotential = paraAttackPotential;
                oneStep = ra.Next(baseStep - attackPotential, attackPotential);/*队员的每步移动距离。它是随机的。*/
            }
            ~Player()
            { 
    
            }
    
            public void showGread()
            {
                label2.Text = "No." + id.ToString() + "is" + gread.ToString();
            }
        }
    }
    
    

    asdasdasdas

             private void button4_Click(object sender, EventArgs e)
            {
                label2.Text = "";
    
                play p = new play();
                for (int i = 0 ; i < p.PlayerNums ; i++)
                {
                     p.GameReady[i].Start(p.PlayerArray[i]);
                }
                /*开启计时器*/
                p.Tim.Start();
            }
    
    点赞 打赏 评论

相关推荐 更多相似问题