VB 2010 - Need to add hours to a timer for a Microwave project-VBForums
Results 1 to 2 of 2

Thread: VB 2010 - Need to add hours to a timer for a Microwave project

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    12

    VB 2010 - Need to add hours to a timer for a Microwave project

    So, my Vb 2010 midterm project is due a 8am ( > 2 hours). I have coded almost everything I need. Just before I was about to submit this, I noticed in the instructions that hours needs to be included as well. I have been trying to do this for about 2hrs now, and just can't seem to get it right.

    I am trying to do 2 things:

    1. I need the display to look like this 0:00:00, populating the placeholders as the user enters in the time
    2. I need the # of hours no great than 9

    Here are the instructions I am trying to adhere to:

    The user should be able to enter a number of hours no greater than 9, a number of minutes no greater than 59 and a number of seconds no greater than 59; otherwise, the invalid cook time will be set to zero.



    Maybe the 6 straight hours working on this has me delirious, but everytime I try to add hours, the program will not even run. I'm not wanting the code given to me, but some guidance here would be very appreciated

    Code:
    Public Class Form1
    
        'contains time entered as a String
        Private timeIs As String = ""
    
        'contains time entered
        Private timeObject As Time
    
        'Button 1 click appends 1 to the time string
        Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    
            Beep() 'sounds beep
            timeIs &= "1" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 2 click appends 2 to the time string
        Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
    
            Beep() 'sounds beep
            timeIs &= "2" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 3 click appends 3 to the time string
        Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
    
            Beep() 'sounds beep
            timeIs &= "3" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 4 click appends 4 to the time string
        Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
    
            Beep() 'sounds beep
            timeIs &= "4" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 5 click appends 5 to the time string
        Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
    
            Beep() 'sounds beep
            timeIs &= "5" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 6 click appends 6 to the time string
        Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
    
            Beep() 'sounds beep
            timeIs &= "6" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 7 click appends 7 to the time string
        Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
    
            Beep() 'sounds beep
            timeIs &= "7" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 8 click appends 8 to the time string
        Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
    
            Beep() 'sounds beep
            timeIs &= "8" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 9 click appends 9 to the time string
        Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
    
            Beep() 'sounds beep
            timeIs &= "9" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'Button 0 click appends 0 to the time string
        Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
    
            Beep() 'sounds beep
            timeIs &= "0" 'append digit to time input
            DisplayTime() 'display the time input 
        End Sub
    
        'starts the microwave's cooking process
        Private Sub Startbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Startbtn.Click
    
            Dim second As Integer
            Dim minute As Integer
    
            'limit timeIs to 4 characters
            timeIs = timeIs.PadLeft(4, Convert.ToChar("0"))
    
            'extract seconds and minutes
            second = Convert.ToInt32(timeIs.Substring(2))
            minute = Convert.ToInt32(timeIs.Substring(0, 2))
    
            'create Time object to contain time entered by user
            timeObject = New Time(minute, second)
    
            displayLabel.Text = String.Format("{0:D2}:{1:D2}",
                timeObject.Minute, timeObject.Second)
    
            timeIs = "" 'clear timeIs for future imput
    
            clockTimer.Enabled = True 'start timer
    
            windowPanel.BackColor = Color.Yellow 'turn "light" on
        End Sub
    
        'Clear button to clear input
        Private Sub Clearbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clearbtn.Click
    
            'reset timer to its initial setting
            displayLabel.Text = "Enter Time"
            foodLabel.Text = ""
            timeIs = ""
            timeObject = New Time(0, 0)
            clockTimer.Enabled = False
            windowPanel.BackColor = Control.DefaultBackColor
        End Sub
    
        'method to display formatted time in timer window
        Private Sub DisplayTime()
    
            Dim second As Integer
            Dim minute As Integer
            Dim hour As Integer
    
            Dim display As String 'string displays current input
    
            'if too much input entered
            If timeIs.Length > 5 Then
                timeIs = timeIs.Substring(0, 5)
            End If
    
            display = timeIs.PadLeft(5, Convert.ToChar("0"))
    
            'extract seconds and minutes
            second = Convert.ToInt32(display.Substring(2))
            minute = Convert.ToInt32(display.Substring(0, 2))
            hour = Convert.ToInt32(display.Substring(0, 1))
    
            'display number of minutes, ":" and number of seconds
            displayLabel.Text = String.Format("{0:D2}:{1:D2}:{2:D2}", hour, minute, second)
        End Sub 'display time
    
        'diplays new time each second that passes
        Private Sub clockTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clockTimer.Tick
    
            clockTimer.Interval = 1000
    
            
    
            'performs countdown, subtract one second
            If timeObject.Second > 0 Then
                My.Computer.Audio.Play(My.Resources._Loop, AudioPlayMode.Background)
                timeObject.Second -= 1
                displayLabel.Text = String.Format("{0:D2}:{1:D2}", timeObject.Minute, timeObject.Second)
            ElseIf timeObject.Minute > 0 Then
                timeObject.Minute -= 1 'subtract one minute
                timeObject.Second = 59 'reset seconds for new minute
                displayLabel.Text = String.Format("{0:D2}:{1:D2}", timeObject.Minute, timeObject.Second)
            Else 'no more seconds
                clockTimer.Enabled = False 'stop timer
                Console.Beep(2000, 3000)
                displayLabel.Text = "Done!" 'inform user time is finished
            End If
            If timeObject.Second > 0 Or
                timeObject.Minute > 0 Then
                foodLabel.Text = "Still Cooking!"
                'My.Computer.Audio.Play(My.Resources._Loop, AudioPlayMode.Background)
            ElseIf timeObject.Second = 0 Then
                windowPanel.BackColor = Control.DefaultBackColor
                foodLabel.Text = "Mr. Carver, your food is hot and ready!"
            End If
        End Sub 'clockTimer_Tick
    End Class 'Microwave
    
    'Time.vb
    'Represents time data and contains properties
    
    Public Class Time
    
        'declare Integers for minute and second
        Private minuteValue As Integer
        Private secondValue As Integer
    
        'Time constructor, minute and second supplied
        Public Sub New(ByVal mm As Integer, ByVal ss As Integer)
    
            Minute = mm 'invokes Minute Set accessor
            Second = ss 'invokes Second Set accessor
        End Sub 'New
    
        'property Minute
        Public Property Minute() As Integer
            'return Minute value
            Get
                Return minuteValue
            End Get 'end of Get accessor
    
            'set Minute Value
            Set(ByVal value As Integer)
                'if minute value entered is valid
                If (value < 60) Then
                    minuteValue = value
                Else
                    minuteValue = 0 'set invalid input to 0
                End If
            End Set 'end of Set accessor
        End Property 'Minute
    
        'property Second
        Public Property Second() As Integer
            'return Second value
            Get
                Return secondValue
            End Get 'end of Get accessor
    
            'set Second Value
            Set(ByVal value As Integer)
                'if second value entered is valid
                If (value < 60) Then
                    secondValue = value
                Else
                    secondValue = 0 'set invalid input to 0
                End If
            End Set 'end of Set accessor
        End Property 'Second
    End Class 'Time

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    7,907

    Re: VB 2010 - Need to add hours to a timer for a Microwave project

    I'd use a DateTimePicker. Init the DateTimePicker like this

    Code:
            DateTimePicker1.Format = DateTimePickerFormat.Custom
            DateTimePicker1.CustomFormat = "H:mm:ss"
            DateTimePicker1.ShowUpDown = True
            DateTimePicker1.Value = New DateTime(2011, 1, 1, 0, 0, 0)
    and then you will only need to guard against hours > 9.

    Code:
        Private Sub DateTimePicker1_ValueChanged(sender As System.Object, _
                                                 e As System.EventArgs) Handles DateTimePicker1.ValueChanged
    
            If DateTimePicker1.Value.Hour > 9 Then
    
                'DateTimePicker1.Value = New DateTime(2011, 1, 1, 0, 0, 0)
                '
                'or
                '
                Dim dt As DateTime = DateTimePicker1.Value
                dt = dt.AddHours(-dt.Hour)
                DateTimePicker1.Value = dt
    
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- SerialPort Answer

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.