Results 1 to 4 of 4

Thread: Need help with this Countdown script please

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    4

    Need help with this Countdown script please

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim startdate As DateTime = DateTime.Parse(Date.Now)
            Dim enddate As DateTime = DateTime.Parse("2016-10-14 13:00")
            Dim ts As TimeSpan = enddate.Subtract(startdate)
    
            Label1.Text = CStr(ts.Days) & " Days, " & CStr(ts.Hours) & " Hours, " & CStr(ts.Minutes) & " Minutes, " & CStr(ts.Seconds) & " Seconds"
            If InStr(1, Label1.Text, "-", vbTextCompare) Then
                Label1.Hide()
            End If
        End Sub
    That is the code. So what I want here is the Timer to be countinousely showed like 10,9,8,7,6. But here it just changes it's value when you click on Button1. So can anyone here help me? I haven't added the "Timer" Component yet. Please provide me with the right script and all that.

    Thank you for your time

  2. #2
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,191

    Re: Need help with this Countdown script please

    Quote Originally Posted by Garfield_Ram View Post
    I haven't added the "Timer" Component yet.
    So why would you think it would work without that? Just move your code to the Timer's Tick event and either enabled the Timer in design time, the Form's Load event or the Button1's Click event.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Need help with this Countdown script please

    On top of what topshot suggested, I'd like to mention that you can tidy up your code where you set the Label's text to:
    Code:
    With ts
        Label1.Text = String.Format("{0} days, {1} hours, {2} minutes, {3} seconds", .Days, .Hours, .Minutes, .Seconds)
    End With
    See how much more tidy and easier to read this code is?

    Also, InStr is a legacy method that really hasn't had a use since prior to 2005 except to maintain code. The method that has replaced InStr is the IndexOf method, but to be honest it looks like you want to check if a hyphen exists and you don't really care for its index, if that is the case then use the Contains method:
    Code:
    If Not Label1.Text.Contains("-") Then
        Label1.Hide()
    End If
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2014
    Posts
    4

    Re: Need help with this Countdown script please

    Thank you everyone! Love you all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width