Results 1 to 2 of 2

Thread: AcceptButton not working?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2010
    Posts
    131

    AcceptButton not working?

    OK, so I am doing something wrong, and I think the slution should be really simple, but that I am not thinking about it. I was just testing the stopwatch from a MSDN example and made a very simple stopwatch. There's three buttons, Start, Reset, Stop. I want that if the stopwatch is not ticking, the acceptbutton would be the start button, if it's running then the acceptbutton should performclick of the stop button, but after the first time, the acceptbutton won't work for any of the buttons, so I am doing something wrong.

    EDIT: Removed some code to make it shorter and edited the way I change the acceptbutton, but the result is the same anyway.
    VB Code:
    1. Imports System
    2. Imports System.Diagnostics
    3. Imports System.Threading
    4.  
    5. Public Class Form1
    6.  
    7.     Dim stopWatch As New Stopwatch()
    8.  
    9.     Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click
    10.         Timer1.Start()
    11.         Me.AcceptButton = StopTime
    12.         End Sub
    13.  
    14.     Private Sub StopTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopTime.Click
    15.        Timer1.Stop() '
    16.         stopWatch.Stop()
    17.         Me.AcceptButton = Start
    18.     Dim ts As TimeSpan = stopWatch.Elapsed
    19.         Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
    20.         Label1.Text = elapsedTime
    21.     End Sub
    22.  
    23.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    24.         stopWatch.Start()
    25.         Dim ts As TimeSpan = stopWatch.Elapsed
    26.         Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
    27.         Label1.Text = elapsedTime
    28.     End Sub
    29.  
    30.     Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click
    31.         stopWatch.Reset()
    32.         Dim ts As TimeSpan = stopWatch.Elapsed
    33.         Dim elapsedTime As String = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10)
    34.         Label1.Text = elapsedTime
    35.     End Sub
    36.  
    37.  End Class
    Last edited by Legjendat; Jul 10th, 2012 at 07:13 PM.

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: AcceptButton not working?

    If you have a command button selected, regardless of what is the current accept button, it will be activated. The current control can override things like this. Think of having a combobox with a CancelButton on the same form, when you dropdown the box and press Esc, the box will cancel it's dropdown. It would be a mightly pain if the form closed .

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