开发者

Status strip and Time

开发者 https://www.devze.com 2022-12-15 03:36 出处:网络
Im creating a game ... I want to add a counter as a clock exactly the same a开发者_高级运维s the Time counter in Microsoft solitair game & i want later when the game is over the time stop and the

Im creating a game ... I want to add a counter as a clock exactly the same a开发者_高级运维s the Time counter in Microsoft solitair game & i want later when the game is over the time stop and the time reached to be saved in a variable what ever but i want to use this number to generate a scores table.

thanx in advance.... I will post the whole game when its 100% done so you guys can Enjoy :-)


You could use a Stopwatch object.

Here's a link on how to use it: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

To start the stopwatch do this:

Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();

To stop:

stopWatch.Stop();
        // Get the elapsed time as a TimeSpan value.
        TimeSpan ts = stopWatch.Elapsed; // gets elapsed time

To show time that's elapsed:

// Format and display the TimeSpan value.
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);

Now you can display the 'elapsedtime' string in your strip.

EDIT:

To see moving time, put following in Button click handler to start time:

    // global variable
        string timeelepse = string.empty; 
   // in button click handler
    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();


            sw.Start();

            System.Threading.Thread t = new System.Threading.Thread(delegate()
            {
                while (true)
                {
                TimeSpan ts = sw.Elapsed;

                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);

                timeelepse = elapsedTime;

                  UpdateLabel();
                }
            });
            t.Start();

Now add these two functions & delegate to your forms class:

public delegate void doupdate();
        public void UpdateLabel()
        {
            doupdate db = new doupdate(DoUpdateLabel);
            this.Invoke(db);
        }

        public void DoUpdateLabel()
        {
            toolStripStatusLabel1.Text = timeelepse;
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消