stopwatch function with button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Syed Hadi
    New Member
    • Dec 2012
    • 56

    #1

    stopwatch function with button

    for this start and stop should there on a button. but i don't want start and stop text on the button. let the button text bi button1 only when we press the button the timer should start on a lable and when we again press the button the timer should stop and the lable time should bi zero. and when we again press it should start .
    does anybody know ......the function what we have to use for it............. .
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    This is not a code writing service but if you post what you have along with a description of the problem and any error messages you get, we can help guide you to a solution.

    Comment

    • Syed Hadi
      New Member
      • Dec 2012
      • 56

      #3
      Code:
      If Button1.Text = "start" Then
                  Button1.Text = "start"
                  'startTime = DateTime.Now()
                  Timer1.Start()
              ElseIf Button1.Text = "start" Then
                  Button1.Text = "start"
                  Timer1.Stop()
                  'endtime = DateTime.Now()
                  Timer1.Start()
              End If

      by this when i m cliking the button one more time the timer is not stoping and not going to zero position
      Last edited by Meetee; Dec 19 '12, 06:04 AM. Reason: Use code tags <code/> around your code

      Comment

      • Syed Hadi
        New Member
        • Dec 2012
        • 56

        #4
        Code:
         If j = 0 Then
                    Timer1.Enabled = True
                    Timer1.Start()
                    j = 1
                Else
                    Timer1.Enabled = False
                    Timer1.Stop()
                    Label1.Text = "00:00:00"
                    j = 0
        i got it but not exact

        Comment

        • Syed Hadi
          New Member
          • Dec 2012
          • 56

          #5
          but it not starting from zero its starting from where it stopped.
          anybody know how the time start from zero with the same button.

          Comment

          • zmbd
            Recognized Expert Moderator Expert
            • Mar 2012
            • 5501

            #6
            Your logic fails: Look at line 2 and line 5 of the code block in post #3.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              How you reset it will depend on which class you're using. There are many timer classes.

              Comment

              • Syed Hadi
                New Member
                • Dec 2012
                • 56

                #8
                clases means you are talking aboout the time intervel period....... and also i want a message box to displayed in a particular time. like for every ten min message box should be displayed but it not comming on every then ten min as the min come to ten the message box is getting started may for every second mesage box is displaying am a freshier.... so plaease guide me ......

                Comment

                • zmbd
                  Recognized Expert Moderator Expert
                  • Mar 2012
                  • 5501

                  #9
                  Doesn't matter what you do until you fix the logic block as I've indicated.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    @z, I believe their next code block fixes the original issue, albeit in an odd way.

                    @syed, I can't answer any of your questions until you answer my question in post #7 because a time interval period is not a class I've heard of nor can I find any such class for VB on the web. You must be using some other class that you need to tell us about.

                    Even if you answer that question, I have no idea what you said in the rest of your post. It is confusing and unclear.

                    Comment

                    • Syed Hadi
                      New Member
                      • Dec 2012
                      • 56

                      #11
                      Code:
                      'Imports System.DateTime
                      
                      Public Class Form1
                          'Dim startTime As DateTime
                          'Dim endtime As DateTime
                          Dim seconds, minutes, hours As Integer
                          Dim j As Integer = 0
                      
                          Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
                              'If (Timer1.Enabled) Then
                              '    Me.Text = "Click to Re-start"
                              '    Timer1.Stop()
                              'Else
                              '    startTime = DateTime.Now()
                              '    Me.Text = "Click to Stop"
                              '    Timer1.Start()
                              '   End If
                          End Sub
                      
                          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                            
                              j = 0 'Then
                              'startTime = DateTime.Now()
                              'Timer1.Enabled = True
                              seconds = 0
                              minutes = 0
                              hours = 0
                      
                              Timer1.Start()
                              '    j = 1
                              'ElseIf j = 1 Then
                              '    Timer1.Stop()
                              '    Timer1.Enabled = False
                              '    j = 0
                              '    Label1.Text = "00:00:00"
                              'End If
                          End Sub
                          
                      
                      
                      
                       
                       
                      
                          Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
                              'Dim span As TimeSpan = DateTime.Now.Subtract(startTime)
                              'Label1.Text = span.Minutes.ToString & ":" & _
                              'span.Seconds.ToString & "." & _
                              'span.Milliseconds.ToString & " "
                              If seconds = 60 Then
                                  seconds = 0
                                  minutes = minutes + 1
                              End If
                              If minutes = 3 Then
                                  minutes = 0
                                  hours = hours + 1
                              End If
                              If minutes = 1 Then
                                  MessageBox.Show("hi")
                              Else
                                  minutes = minutes + 1
                              End If
                      
                              seconds = seconds + 1
                              Label1.Text = Format(hours, "00") & "." & Format(minutes, "00") & "." & Format(seconds, "00")
                          End Sub
                          
                      End Class

                      see sir this is my all coding

                      Comment

                      • gumpagoo
                        New Member
                        • Dec 2012
                        • 3

                        #12
                        LOL you guys are fighting a lost cause. Not much you can do without the correct info and you are right fix the logic try again post the next error. LOL

                        Comment

                        • Rabbit
                          Recognized Expert MVP
                          • Jan 2007
                          • 12517

                          #13
                          I can't make heads or tails of your code. You've commented out almost everything. I don't think your code will do anything at all much less the behaviour you described.

                          Comment

                          • zmbd
                            Recognized Expert Moderator Expert
                            • Mar 2012
                            • 5501

                            #14
                            Syed Hadi:
                            If Rabbit had not replied, I would have deleted this as a homework question.

                            As your postings have progressed, the entire thread continues to give me the impression that this is a homework question and as such we should stop here until you can provide an example that actually works without basic logical errors or jumbled up half code.

                            Your initial code had a fundamental logic error.
                            The second code doesn't handle that logic error.
                            The third code does little if anything as Rabbit has pointed out.

                            Therefore, I respectfully ask that you take a few deep breaths, regroup, and then take a very hard close look at your project.

                            1) Write out in a single paragraph the major goals that you want the program to accomplish. This should be in "plain English." Not that you have to use English, just that you should not use any pseudo code, jargon, or other short cuts.

                            2) In a single paragraph, for each of the major goals, detail the steps. These also should be in "plain English."

                            3) You need to do a logic chart for your program. I prefer the NS style ( nassi shneiderman diagram Google Search ) however any programing flow-charting method will work. This should flow naturally from your work in (1) and (2)

                            4) Using the flowchart write the major blocks of code and then fill in with your support code as needed.

                            5) Compile and fix compiler-time errors.

                            6) Execute the program

                            7) Then come back here with any further issues.

                            Comment

                            • Syed Hadi
                              New Member
                              • Dec 2012
                              • 56

                              #15
                              k
                              from next time ill

                              Comment

                              Working...