Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

timer displayed on label must count backwards

Posted on 2004-09-15
5
397 Views
Last Modified: 2008-02-01
hi, i have a form with a timer and a label
the idea is that the text of the table is set to 00:15:00
and then it counts backwards to 0, and each second , it counts down.
the problem is with the string manipulation
i use vb6's left function, and i get the string to not display AM at the end because it would have displayed
12:14:59AM
now i need to remove the first 3 char's as well so that it only shows 14:59 and counts from there
 
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = 1000
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = Microsoft.VisualBasic.Left(DateAdd("s", -1, Label1.Text), 8)
        Label1.Text = Microsoft.VisualBasic.Right(Label1.Text, 5)
    End Sub

this current setup does not give a error but does not display it right because first i strip the last charaters off,
and then i still need the original value to calculate what the next value will be.
should i use a second timer? is there a way to use ms.vb.left & ms.vb.right right in one command?
thanks!
0
Comment
Question by:jxharding
  • 4
5 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 12062163
you are in vb.net so better pick a .Net way if there is. Use the AddSeconds(-n) function to achieve this :

dim dt as DateTime = DateTime.Now
dt.AddSeconds(-1)

I think you will get rid of the string manipulation
0
 
LVL 19

Expert Comment

by:Desp
ID: 12062402
this should fix the problem

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
     Label1.Text = Convert.ToDateTime(textBox1.Text).AddSeconds(-1).ToLongTimeString()
 End Sub

assuming Label1 has some valid time string before running this code
0
 

Author Comment

by:jxharding
ID: 12062432
hi desp,
i tried both fixes ,and although they make perfect sense, i still get
12:14:59AM
on both
did your's display correctly?
i tried having the text on the label as
00:15:00
and  00:15
thanks!
0
 
LVL 19

Accepted Solution

by:
Desp earned 250 total points
ID: 12062602
Label1.Text = Convert.ToDateTime(textBox1.Text).AddSeconds(-1).TimeOfDay.ToString() ;
0
 
LVL 19

Expert Comment

by:Desp
ID: 12062608
replace textbox1 by label1 ... in the previous comment
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 38
SQL LINE CONTINUATION ISSUE 12 37
get row value in vb.net 4 17
Help with error in Query 2 31
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question