Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can someone help me with my problem?
I have 3 labels:

  • 1st Label = Show the down time (No concern)
  • 2nd Label = show the idle time (No concern)
  • 3rd Label = I need the sum of label 1 and label 2

But when I tried to add Label 1 and Label 2, I encountered the error.

Can someone help me with what program I can use? Using VB.NET.

What I have tried:

VB.NET
Private Sub TStop_Tick(sender As Object, e As EventArgs) _
        Handles TStop.Tick
        Label1.Text = aa.ToString("00:") & bb.ToString("00:") & cc.ToString("00")
        cc = cc + 1
        If cc > 59 Then
            cc = 0
            bb = bb + 1
        ElseIf bb > 59 Then
            bb = 0
            aa = aa + 1
        End If

Private Sub TIdle_Tick(sender As Object, e As EventArgs) Handles TIdle.Tick
        Label2.Text = dd.ToString("00:") & ee.ToString("00:") & ff.ToString("00")
        ff = ff + 1
        If ff > 59 Then
            ff = 0
            ee = ee + 1
        ElseIf ee > 59 Then
            ee = 0
            dd = dd + 1
        End If
    End Sub

Dim gg, hh, ii As Double
        gg = Label1.Text
        hh = Label2.Text
        Label3.Text = ii

        Label3.Text = gg + hh
Posted
Updated 28-Aug-23 9:37am
v2
Comments
Dave Kreskowiak 26-Aug-23 0:52am    
Why do you have time values split into individual variables? A time value should be in a single value to make it easier to work with.

1 solution

The problem is that neither Label1 nor Label2 contain a Double value: They contain a string with your time separated by colons: "12:35:56", or "23:00:00" - and the system will throw an exception if you try to convert that to a Double. Even if it didn't, adding two doubles will not give you what you need as adding 10 seconds to 55 seconds will not give you 1 minute and 5 seconds - it will give you 65 seconds.

To do this, you would need to parse the string into a Timespan and add those:
Dim s as String = aa.ToString("00:") & bb.ToString("00:") & cc.ToString("00")
Console.WriteLine(s)
Dim ts as TimeSpan = TimeSpan.Parse(s)
Console.WriteLine(ts)
Dim result as TimeSpan = ts + ts
Console.WriteLine(result)
 
Share this answer
 
Comments
Marlon Rivera 26-Aug-23 1:54am    
But how can I show the value in the Label3?
OriginalGriff 26-Aug-23 2:43am    
You are kidding, right?
You *really* can't write a trivial line of code on your own?
Label3.Text = result.ToString()


* Sorry about that - the cat walked on the mouse and sent it.
Marlon Rivera 26-Aug-23 4:24am    
Thank you here is my final code:
Dim time1 As TimeSpan = TimeSpan.Parse(Label1.Text)
Dim time2 As TimeSpan = TimeSpan.Parse(Label2.Text)

' Add the time values together
Dim result As TimeSpan = time1.Add(time2)

' Display the result in TextBox3
Label3.Text = result.ToString()
OriginalGriff 26-Aug-23 4:52am    
Why are you using ".Add"? "+" is much clearer and easier to read ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900