i have an repeater there is multiple record each record have different expiry datetime.
i want to set timer with remaining time for each record in timer to show remaining time continue.
Please help me with code.
coding on vb.net
i have an repeater there is multiple record each record have different expiry datetime.
i want to set timer with remaining time for each record in timer to show remaining time continue.
Please help me with code.
coding on vb.net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Abhishek YadavRank #217 · 11.3K reputationPosted Feb 10, 2024, 2:11 AM EST
Imports System.Timers Public Class Form1 Private records As New List(Of Record)() Private timers As New List(Of Timer)() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Sample records with different expiry datetimes records.Add(New Record("Record 1", DateTime.Now.AddSeconds(20))) records.Add(New Record("Record 2", DateTime.Now.AddSeconds(30))) records.Add(New Record("Record 3", DateTime.Now.AddSeconds(40))) ' Create a timer for each record For Each record In records Dim timer As New Timer() timers.Add(timer) ' Set the interval of the timer to 1 second (1000 milliseconds) timer.Interval = 1000 ' Add the Tick event handler for the timer AddHandler timer.Elapsed, Sub(sender, args) TimerTick(sender, args, record) ' Start the timer timer.Start() Next End Sub Private Sub TimerTick(sender As Object, e As ElapsedEventArgs, record As Record) ' Calculate the remaining time Dim remainingTime = record.ExpiryDateTime - DateTime.Now ' Update the UI on the main thread BeginInvoke(Sub() ' Update the remaining time in the UI for the corresponding record If remainingTime.TotalSeconds > 0 Then ' Display remaining time label1.Text = $"{record.Name}: {remainingTime.ToString("h\:mm\:ss")}" Else ' Expiry time reached label1.Text = $"{record.Name}: Expired" End If End Sub) End Sub End Class Public Class Record Public Property Name As String Public Property ExpiryDateTime As DateTime Public Sub New(name As String, expiryDateTime As DateTime) Me.Name = name Me.ExpiryDateTime = expiryDateTime End Sub End ClassHere's an updated code snippet that shows how to update the time without reloading the page using a Timer:
Abhishek YadavRank #217 · 11.3K reputationPosted Feb 10, 2024, 1:41 AM EST
Imports System.Timers Public Class Form1 Private records As New List(Of Record)() Private timers As New List(Of Timer)() Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' Sample records with different expiry datetimes records.Add(New Record("Record 1", DateTime.Now.AddSeconds(20))) records.Add(New Record("Record 2", DateTime.Now.AddSeconds(30))) records.Add(New Record("Record 3", DateTime.Now.AddSeconds(40))) ' Create a timer for each record For Each record In records Dim timer As New Timer() timers.Add(timer) ' Set the interval of the timer to 1 second (1000 milliseconds) timer.Interval = 1000 ' Add the Tick event handler for the timer AddHandler timer.Elapsed, Sub(sender, args) TimerTick(sender, args, record) ' Start the timer timer.Start() Next End Sub Private Sub TimerTick(sender As Object, e As ElapsedEventArgs, record As Record) ' Calculate the remaining time Dim remainingTime = record.ExpiryDateTime - DateTime.Now ' Update the UI on the main thread BeginInvoke(Sub() ' Update the remaining time in the UI for the corresponding record If remainingTime.TotalSeconds > 0 Then ' Display remaining time label1.Text = $"{record.Name}: {remainingTime.ToString("h\:mm\:ss")}" Else ' Expiry time reached label1.Text = $"{record.Name}: Expired" End If End Sub) End Sub End Class Public Class Record Public Property Name As String Public Property ExpiryDateTime As DateTime Public Sub New(name As String, expiryDateTime As DateTime) Me.Name = name Me.ExpiryDateTime = expiryDateTime End Sub End Classrupesh gourRank #6,510 · 264 reputationPosted Feb 10, 2024, 1:48 AM EST
i want update time without page load , with timer page load is happening.