5 Replies - 2085 Views - Last Post: 20 November 2011 - 09:12 PM Rate Topic: -----

#1 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Enhanced Time: Increasing minute, second and hour on button click

Posted 20 November 2011 - 07:19 PM

alright my ? is kinda simple i think and makes me feel dumb lol i have this GUI with set time button and Increment seconds right well my ? is this i need to add a increment hour and increment min button, here is the code :
my ? is this how do i make the two new buttons work like code wise ?? this is attempt like 5 everytime ive played with the code and added buttons ive crashed the program so please help me !! =] also it just adds 1 to the mins and hours
Public Class TimeTest
   Dim time As New Time() ' construct Time with zero arguments

   ' invoked when user clicks the Add 1 to Second button
   Private Sub addSecondButton_Click(ByVal sender As System.Object,
      ByVal e As System.EventArgs) Handles incrementSecondButton.Click

      time.Second = (time.Second + 1) Mod 60 ' add 1 to Second

      ' add one minute if 60 seconds have passed
      If time.Second = 0 Then
         time.Minute = (time.Minute + 1) Mod 60 ' add 1 to Minute

         ' add one hour if 60 minutes have passed
         If time.Minute = 0 Then
            time.Hour = (time.Hour + 1) Mod 24 ' add 1 to Hour
         End If
      End If

      UpdateDisplay() ' update the TextBoxes and output Labels
   End Sub ' addSecondButton_Click

   ' set time based on TextBox values
   Private Sub setTimeButton_Click(ByVal sender As System.Object,
      ByVal e As System.EventArgs) Handles setTimeButton.Click

      ' ensure that hour, minute and second are in range
      Try
         If setHourTextBox.Text <> String.Empty Then
            time.Hour = Convert.ToInt32(setHourTextBox.Text)
         End If

         If setMinuteTextBox.Text <> String.Empty Then
            time.Minute = Convert.ToInt32(setMinuteTextBox.Text)
         End If

         If setSecondTextBox.Text <> String.Empty Then
            time.Second = Convert.ToInt32(setSecondTextBox.Text)
         End If
      Catch ex As ArgumentOutOfRangeException
         MessageBox.Show("The hour, minute or second was out of range",
            "Out of Range", MessageBoxButtons.OK, MessageBoxIcon.Error)
      End Try

      UpdateDisplay() ' update the TextBoxes and output Labels
   End Sub ' setTimeButton_Click

   ' update time display
   Private Sub UpdateDisplay()
      setHourTextBox.Text = Convert.ToString(time.Hour)
      setMinuteTextBox.Text = Convert.ToString(time.Minute)
      setSecondTextBox.Text = Convert.ToString(time.Second)
      output1Label.Text = ("Hour: " & time.Hour & "; Minute: " &
      time.Minute & "; Second: " & time.Second)
      output2Label.Text = ("Standard time is: " & time.ToString() &
      "; Universal Time is: " & time.ToUniversalString())
   End Sub ' UpdateDisplay
End Class ' TimeTest




This post has been edited by smohd: 20 November 2011 - 09:16 PM
Reason for edit:: Strength the title


Is This A Good Question/Topic? 1
  • +

Replies To: Enhanced Time: Increasing minute, second and hour on button click

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Enhanced Time: Increasing minute, second and hour on button click

Posted 20 November 2011 - 08:19 PM

Quote

is this how do i make the two new buttons work like code wise ?? this is attempt like 5 everytime ive played with the code and added buttons ive crashed the program so please help me !! =]

SO what should we help you with? You know, I didnt understand the question well, can you please explain? Also use complete words not "ive".
Was This Post Helpful? 1
  • +
  • -

#3 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Re: Enhanced Time: Increasing minute, second and hour on button click

Posted 20 November 2011 - 08:24 PM

alright sorry my ? is how do i make the two added buttons "increment hours" and "increment minutes" i jsut wanted to know how i should code them. the ? is how do i get them to work by adding one to the hour when u click it and one to the min when u click it see like this snippet :
Private Sub addSecondButton_Click(ByVal sender As System.Object,  

      ByVal e As System.EventArgs) Handles incrementSecondButton.Click  

   

       time.Second = (time.Second + 1) Mod 60 ' add 1 to Second  

   

       ' add one minute if 60 seconds have passed  

       If time.Second = 0 Then 

          time.Minute = (time.Minute + 1) Mod 60 ' add 1 to Minute  

    
          ' add one hour if 60 minutes have passed  

        If time.Minute = 0 Then 

             time.Hour = (time.Hour + 1) Mod 24 ' add 1 to Hour  

          End If 

       End If   

       UpdateDisplay() ' update the TextBoxes and output Labels  

    End Sub ' addSecondButton_Click  



so would the increment hours and mins buttons be set up like this but in reverse of some of these ??
Was This Post Helpful? 1
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Enhanced Time: Increasing minute, second and hour on button click

Posted 20 November 2011 - 08:35 PM

In your mins button, it will be the same concept except now you will start by adding the mins and check like what you checked above. But you will ignore the seconds part.

And in hour also you will deal with adding hours only. That is how they will look like.
Was This Post Helpful? 1
  • +
  • -

#5 moparman426  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 32
  • Joined: 08-September 11

Re: Enhanced Time: Increasing minute, second and hour on button click

Posted 20 November 2011 - 09:07 PM

Thank you all it works and fine here is code for anyone else that might need it :

Public Class TimeTest
   Dim time As New Time() ' construct Time with zero arguments

   ' invoked when user clicks the Add 1 to Second button
   Private Sub addSecondButton_Click(ByVal sender As System.Object,
      ByVal e As System.EventArgs) Handles incrementSecondButton.Click

      time.Second = (time.Second + 1) Mod 60 ' add 1 to Second
        If time.Second = 0 Then
            time.Minute = (time.Minute + 1) Mod 60 ' add 1 to Minute  
        End If

        UpdateDisplay() ' update the TextBoxes and output Labels
    End Sub ' addSecondButton_Click
    Private Sub addHourButton_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles incrementHourButton.Click
        time.Hour = (time.Hour + 1) Mod 24
        
        UpdateDisplay()
    End Sub
    Private Sub addMinuteButton_Click(ByVal sender As System.Object,
        ByVal e As System.EventArgs) Handles incrementMinuteButton.Click
        time.Minute = (time.Minute + 1) Mod 24
        If time.Minute = 0 Then
            time.Hour = (time.Hour + 1) Mod 24
        End If
        UpdateDisplay()
    End Sub

    ' set time based on TextBox values
    Private Sub setTimeButton_Click(ByVal sender As System.Object,
       ByVal e As System.EventArgs) Handles setTimeButton.Click

        ' ensure that hour, minute and second are in range
        Try
            If setHourTextBox.Text <> String.Empty Then
                time.Hour = Convert.ToInt32(setHourTextBox.Text)
            End If

            If setMinuteTextBox.Text <> String.Empty Then
                time.Minute = Convert.ToInt32(setMinuteTextBox.Text)
            End If

            If setSecondTextBox.Text <> String.Empty Then
                time.Second = Convert.ToInt32(setSecondTextBox.Text)
            End If
        Catch ex As ArgumentOutOfRangeException
            MessageBox.Show("The hour, minute or second was out of range",
               "Out of Range", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        UpdateDisplay() ' update the TextBoxes and output Labels
    End Sub ' setTimeButton_Click

    ' update time display
    Private Sub UpdateDisplay()
        setHourTextBox.Text = Convert.ToString(time.Hour)
        setMinuteTextBox.Text = Convert.ToString(time.Minute)
        setSecondTextBox.Text = Convert.ToString(time.Second)
        output1Label.Text = ("Hour: " & time.Hour & "; Minute: " &
        time.Minute & "; Second: " & time.Second)
        output2Label.Text = ("Standard time is: " & time.ToString() &
        "; Universal Time is: " & time.ToUniversalString())
    End Sub ' UpdateDisplay
End Class ' TimeTest




Was This Post Helpful? 2
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Enhanced Time: Increasing minute, second and hour on button click

Posted 20 November 2011 - 09:12 PM

Glad you get it :)
Except one correction:
- in your addMinuteButton_Click() sub, line 23 should read:
 time.Minute = (time.Minute + 1) Mod 60

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1