Results 1 to 8 of 8

Thread: Number In Label converted to time.

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Exclamation Number In Label converted to time.

    Hello,
    Anyone have any idea how I can take a label that has a number lets say 90 and convert it to time so that means it would display 1:30 but I dont want to do 90/60 =1.50 I need the label (Seconds) to display in time. Right Now I'm looking to have that display with label3

    Code:
    Option Explicit On
    Public Class Form1
        Dim SecondsDifference As Integer = 2500
        Dim hms = TimeSpan.FromSeconds(SecondsDifference)
        Dim h = hms.Hours.ToString
        Dim m = hms.Minutes.ToString
        Dim s = hms.Seconds.ToString
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strInput As String
            strInput = RichTextBox1.Text
            Dim strSplit() As String
            strSplit = strInput.Split(CChar(" "))
            Label2.Text = "Number of Words: " & strSplit.Length
            ' Label1.Text = Math.Round(Val(Label1.Text), 2)
            ' Label3.Text = "Read Length: " & strSplit.Length / 3
            Label3.Text = "Read Time :" & CStr(Math.Round(strSplit.Length / TextBox1.Text)) & " Seconds"
            ' If Label3.text > 61 Then / 60
            ' End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            RichTextBox1.Text = ""
            Label2.Text = "Number of Words:"
            Label3.Text = "Read Time:"
        End Sub
    
        Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                Button1.PerformClick()
            End If
    
    
        End Sub
    
    End Class

  2. #2

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Number In Label converted to time.

    This is the actual code that works as of now..

    Code:
    Option Explicit On
    Public Class Form1
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strInput As String
            strInput = RichTextBox1.Text
            Dim strSplit() As String
            strSplit = strInput.Split(CChar(" "))
            Label2.Text = "Number of Words: " & strSplit.Length
            ' Label1.Text = Math.Round(Val(Label1.Text), 2)
            ' Label3.Text = "Read Length: " & strSplit.Length / 3
            Label3.Text = "Read Time :" & CStr(Math.Round(strSplit.Length / TextBox1.Text)) & " Seconds"
            ' If Label3.text > 61 Then / 60
            ' End If
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            RichTextBox1.Text = ""
            Label2.Text = "Number of Words:"
            Label3.Text = "Read Time:"
        End Sub
    
        Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                Button1.PerformClick()
            End If
    
    
        End Sub
    
    End Class

  3. #3
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Number In Label converted to time.

    Your code is really confusing.
    What would help would be so see a sample of the input for the richtextbox and the textbox and what you would want the output to be for that sample.

    If your input is always elapsed seconds what do you want your output to be? Hours:Minutes:Seconds?
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Number In Label converted to time.

    Basically I'm trying to display how long it takes to read words in a script. So what happens is this counts the number of words that are typed and divided that by in most cases 3(Avg words read in 1 second) but that can be changed in the text box.

    So if the number of words divided by read time is under 60 we are good.
    But if it is over it needs to be displayed in time.
    If it was 90 seconds it needs to display 1:30

    Name:  Screen Shot 2015-04-11 at 5.03.52 PM.jpg
Views: 139
Size:  15.4 KB

    Updated Code:
    Code:
    Option Explicit On
    Public Class Form1
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim strInput As String
            strInput = RichTextBox1.Text
            Dim strSplit() As String
            strSplit = strInput.Split(CChar(" "))
            Label2.Text = "Number of Words: " & strSplit.Length
            Label3.Text = "Read Time :" & CStr(Math.Round(strSplit.Length / TextBox1.Text))
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            RichTextBox1.Text = ""
            Label2.Text = "Number of Words: "
            Label3.Text = "Read Time"
        End Sub
    
        Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles RichTextBox1.KeyDown
            If e.KeyCode = Keys.Enter Then
                Button1.PerformClick()
            End If
        End Sub
    End Class

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Number In Label converted to time.

    Use the numeric value in the label to create a TimeSpan object, then call its ToString method...

    Code:
      Dim t As Integer = 90' assuming the value represent seconds...
    Dim ts As New TimeSpan(0, 0, t)
    MessageBox.Show(ts.ToString)
    Last edited by kebo; Apr 11th, 2015 at 05:22 PM. Reason: added time span msdn link
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Number In Label converted to time.

    Thanks for the help. It worked! This is how I used it.

    Code:
            Dim t As Integer = CStr((strSplit.Length / TextBox1.Text)) 
            Dim ts As New TimeSpan(0, 0, t)
            Label5.Text = (ts.ToString)

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: Number In Label converted to time.

    Glad that it's working, but code that "works" is not necessarily good code. In your code, you are dividing an integer by a string, converting that to a string, then assigning the string to an integer. That's like ironing your computer before you eat you toaster for breakfast. Turn on Option Strict, fix the errors, and you will have a much better solution.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8

    Thread Starter
    Member
    Join Date
    Apr 2015
    Posts
    42

    Re: Number In Label converted to time.

    K. I'm not good at this at all just trying to make it work for now. haha But yeah I know the code is bad.

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