Results 1 to 4 of 4

Thread: Help with text boxes and timer controls

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2007
    Posts
    27

    Help with text boxes and timer controls

    Hello, I am very new to Visual Basic and have hit a couple snags. Below is all of my code. I am supposed to be making a program like a microwave. The text box that displays the time should only show 2 numbers for the minutes, a ":" and 2 numbers for the seconds. I have set the max length for the text box to 5 but when the buttons are clicked more than five numbers will show up. Also, the ":" doesn't stay in the middle, I have tried to use the substring to break the numbers up, but that isn't working for me.

    My other problem is that this time that can be entered is also supposed to run like a timer. I have added the timer control and think that I need to use the interval to make this work, but I am not sure. I can't get the numbers back from the text box as an integer for that to work because option strict and VB won't let me....

    Any help is really appreciated...

    Champ0342


    Option Strict On
    Public Class Form1
    Dim b(9) As String
    Private Sub form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim display(9) As String
    'button values
    b(0) = "0"
    b(1) = "1"
    b(2) = "2"
    b(3) = "3"
    b(4) = "4"
    b(5) = "5"
    b(6) = "6"
    b(7) = "7"
    b(8) = "8"
    b(9) = "9"

    tbtime.Text = "00:00"
    tbtime.MaxLength = 5

    End Sub

    Private Sub btnone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnone.Click

    tbtime.Text = tbtime.Text & b(1)

    End Sub

    Private Sub btntwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntwo.Click

    tbtime.Text = tbtime.Text & b(2)

    End Sub

    Private Sub btnthree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthree.Click

    tbtime.Text = tbtime.Text & b(3)

    End Sub

    Private Sub btnfour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfour.Click

    tbtime.Text = tbtime.Text & b(4)

    End Sub

    Private Sub btnfive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfive.Click

    tbtime.Text = tbtime.Text & b(5)

    End Sub

    Private Sub btnsix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsix.Click

    tbtime.Text = tbtime.Text & b(6)

    End Sub

    Private Sub btnseven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseven.Click

    tbtime.Text = tbtime.Text & b(7)

    End Sub

    Private Sub btneight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneight.Click

    tbtime.Text = tbtime.Text & b(8)

    End Sub

    Private Sub btnnine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnine.Click

    tbtime.Text = tbtime.Text(0) & b(9)

    End Sub

    Private Sub btnzero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnzero.Click

    tbtime.Text = tbtime.Text & b(0)

    End Sub

    Private Sub btnstart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstart.Click

    tmrtime.Enabled = True

    If tmrtime.Enabled = True Then
    pbwindow.BackColor = Color.Yellow
    End If

    End Sub

    Private Sub btnstop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstop.Click
    Dim pause As String
    pause = "Pause"

    tmrtime.Enabled = False

    If tmrtime.Enabled = False Then
    pbwindow.BackColor = Color.LightGray
    End If

    If tmrtime.Enabled = False Then
    tbtime.Text = pause
    End If

    End Sub

    Private Sub tbtime_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbtime.TextChanged
    tbtime.MaxLength = 5
    End Sub
    End Class

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with text boxes and timer controls

    try this

    Code:
    Public Class form1
    
        Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.ReadOnly = True
            TextBox1.Text = "00:00"
        End Sub
    
        Private Sub btnAddMinute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMinute.Click
            TextBox1.Text = TextBox1.Text.Substring(0, 3) & CType(Format(CType(TextBox1.Text.Substring(3, 2), Integer) + 1, "00"), String)
        End Sub
    
        Private Sub btnAddHour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddHour.Click
            TextBox1.Text = CType(Format(CType(TextBox1.Text.Substring(0, 2), Integer) + 1, "00"), String) & TextBox1.Text.Substring(2, 3)
        End Sub
    
    End Class

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with text boxes and timer controls

    You should be using a TimeSpan object to store the time, then adding minutes and seconds to that value. You can then display the contents of that TimeSpan each time it changes.

    As for using a Timer, many people are under the misconception that they are for measuring time. They are NOT. A Timer works like an alarm, NOT a stopwatch. If you want the display updated every second then you should use a Timer to trigger the update, but the Timer does NOT measure the elapsed time. For that you should use a TimeSpan object and a Stopwatch object. For instance, if you want to count down 5 minutes, you would create a TimeSpan object that represented 5 minutes. You then star a Stopwatch AND a Timer. Each time the Timer raised a Tick event you would get the Elapsed time from the Stopwatch, subtract it from the original TimeSpan and then display the result to the user.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Help with text boxes and timer controls

    this works

    Code:
    Public Class form1
    
        Dim timeStr As String = ""
        Dim numbersEntered As String = ""
        Dim tempStr As String = ""
    
        Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.ReadOnly = True
            TextBox1.Text = "00:00"
            Timer1.Interval = 1000
        End Sub
    
        Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
            If TextBox1.Text = "paused" Then TextBox1.Text = timeStr
            Timer1.Enabled = True
        End Sub
    
        Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
            Timer1.Enabled = False
            TextBox1.Text = "paused"
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Dim strMinutes As String = CType(Format(CType(TextBox1.Text.Substring(3, 2), Integer) - 1, "00"), String)
            Dim strHour As String = Format(CType(TextBox1.Text.Substring(0, 2), Integer), "00")
            If CType(strMinutes, Decimal) = -1 Then
                If CType(TextBox1.Text.Substring(0, 2), Integer) > 0 Then
                    strMinutes = 59
                    strHour = CType(Format(CType(TextBox1.Text.Substring(0, 2), Integer) - 1, "00"), String)
                    timeStr = strHour & ":" & strMinutes
                    TextBox1.Text = timeStr
                Else
                    Timer1.Enabled = False
                    timeStr = "00:00"
                    TextBox1.Text = timeStr
                End If
            Else
                timeStr = strHour & ":" & strMinutes
                TextBox1.Text = timeStr
            End If
        End Sub
    
        Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
            If tempStr.Length < 4 Then
                tempStr += "1"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
            If tempStr.Length < 4 Then
                tempStr += "2"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
            If tempStr.Length < 4 Then
                tempStr += "3"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
            If tempStr.Length < 4 Then
                tempStr += "4"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
            If tempStr.Length < 4 Then
                tempStr += "5"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
            If tempStr.Length < 4 Then
                tempStr += "6"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
            If tempStr.Length < 4 Then
                tempStr += "7"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
            If tempStr.Length < 4 Then
                tempStr += "8"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
            If tempStr.Length < 4 Then
                tempStr += "9"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
            If tempStr.Length < 4 Then
                tempStr += "0"
                numbersEntered = tempStr
            End If
            numbersEntered = New String("0", 4 - numbersEntered.Length) & numbersEntered
            TextBox1.Text = numbersEntered.Substring(0, 2) & ":" & numbersEntered.Substring(2, 2)
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            tempStr = ""
            TextBox1.Text = "00:00"
        End Sub
    End Class

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