countdown timer

karunam

Member
Joined
Aug 1, 2005
Messages
11
Programming Experience
Beginner
hi ,
need to display countdown timer i.e for given (hr:mi:ss) as parameter.This is to show left out time.


Thanks in advance
karuna
 
use DateTime variable and DateDiff function
 
I'd suggest not using the DateDiff function. If you want to display a countdown for 10 minutes, for example, then create a Timer object with an Interval of 1000 (1 second) and use code something like this:
VB.NET:
    Private alarmTime As Date

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.alarmTime = Date.Now.AddMinutes(10)
        Me.Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If alarmTime < Date.Now Then
            Me.Timer1.Stop()
            MessageBox.Show("Time's up.")
        Else
            Dim remainingTime As TimeSpan = Me.alarmTime.Subtract(Date.Now)

            Me.Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", _
                                           remainingTime.Hours, _
                                           remainingTime.Minutes, _
                                           remainingTime.Seconds)
        End If
    End Sub
 
Nice complete example jmcolhinney, but I have a few comments.

I agree to avoid use of DateDiff function since it's a legacy function (VB6 compability - old habits..).
But you use the Date data type and it's subtract method which is also legacy (VB6), so I don't see any difference to that suggestion.

The corresponding .Net Framework type of Date is DateTime.
The DateTime has got an op_Subtraction Method that returns a TimeSpan class object with days/hours/minutes/seconds etc.
 
JohnH said:
But you use the Date data type and it's subtract method which is also legacy (VB6), so I don't see any difference to that suggestion.
JohnH, you are working under a serious misconception. The Date type is an in-built VB.NET type, just like Integer, String, Char, Boolean, Double, Decimal, Single, etc. Note that all those keywords turn blue in the IDE when you type them as they are part of the VB.NET language itself. Each of those native types is implemented using a .NET Framework type. The String type is implemented using the System.String class, the Integer type is implemeted using the System.Int32 structure and the Date type is implemented using the System.DateTime structure. This means that when you declare a variable as the Date type it IS a System.DateTime object, just like when you declare a variable of type Integer it IS a System.Int32 object. In my code, every member of the Date type that I use IS a member of the System.DateTime structure. By all means look up the member listing for the System.DateTime structure and you'll see that it has Now, AddMinutes and Subtract members. The in-built VB.NET types have nothing to do with legacy support and everything to do with the standard types that every self-respecting programming language supports.
 
What is that quote suppoesed to be telling me? I already know that the Date type and the System.DateTime type are equivalent. A Date object and a System.DateTime object are exactly the same thing, as the attached screenshot shows. When you declare an integer variable, do you declare it as Integer or System.Int32? I'll wager you use Integer, but there is exactly the same relationship between Integer and System.Int32 as there is between Date and System.DateTime.
 

Attachments

  • Date.JPG
    Date.JPG
    6.6 KB · Views: 393
I had to read this article ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconDataTypeChanges.asp ) again before I discovered my short-circuit..
The story was a VB6 date data type called "Date", which was eight bytes double format. In .Net this data type was dumped and they created a new date data type and named it "Date", which was eight bytes integer format.
Through the DateTime structure (= the implementation of the Date data type ((meaning all data types is an interface?))) we can if needed convert our integer Date to/from a double Date with the ToOADate and FromOADate methods.
So I am hereby excused from the lecture, I think ;)
 
All the in-built VB.NET types (Date, Integer, String, etc.) are simply aliases for .NET Framework types (DateTime, Int32, String, etc.). Some have the same name, like String and System.String, but you know when you are using an in-built type because the keyword will turn blue. I'm a big fan of consistency so I never use the .NET type when there is an equivalent inbuilt type. As I said, I'd bet that pretty much everyone would use Integer over Int32, so to be consistent you need to use Date over DateTime, etc.
 
Back
Top