-
Oct 14th, 2016, 05:02 AM
#1
Thread Starter
New Member
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
-
Oct 14th, 2016, 07:59 AM
#2
Re: Need help with this Countdown script please
Originally Posted by Garfield_Ram
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.
-
Oct 14th, 2016, 08:27 AM
#3
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
-
Oct 14th, 2016, 10:03 AM
#4
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|