none
Play/Stop button using only one button

    Question

  • I want to know how to make a play/stop button using only one button.  Such as Windows Media Player or Itunes does with their play/pause button.  Thanks for the help. 

    Vance Lee
    Microsoft Visual .NET 2005

    If this is in the wrong forum, can someone point me to the correct forum.  Sorry.
    Friday, December 07, 2007 1:56 AM

Answers

  •  

    A comment about what you doing - its very poor because you are using thread.sleep.    I cannot state this enough  USING THREAD.SLEEP IS A VERY BAD WAY TO CAUSE A DELAY IN AN APPLICATION.

     

    In you application for the vast majority of the time is spent in the sleep state meaning the UI will be completely dead and unresponsive to imput.    You should start thinking in terms of events.

     

    As stated numerous timers in this forums about delays/pauses/waits use of a timer is a much better approach and allows the UI to remain responsive.     A better approach using a counter and a timer is included which allows the process to be started and stopped at any time - the UI remains responsive throughout.   The speed of the UI change can be modified by simply modifying the interval property.

     

    Code Block

    Public Class Form1
        Dim Counter As Integer = 0


       
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 1000
            Button1.Text = "Start"

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
           
            Select Case Counter Mod 4
                Case 0
                    'Play Sounds
                    My.Computer.Audio.Play("d:\WINDOWS\Media\ding.wav", AudioPlayMode.Background)
                    cbx(False, False, True, False, False)
                Case 1
                    cbx(False, False, False, True, False)
                Case 2
                    cbx(False, False, False, False, True)
                Case 3
                    cbx(False, False, False, True, False)
            End Select
            Counter += 1

        End Sub

     

        Private Sub cbx(ByVal a, ByVal b, ByVal c, ByVal d, ByVal e)
            ComboBox1.Visible = a
            ComboBox2.Visible = b
            ComboBox3.Visible = c
            ComboBox4.Visible = d
            ComboBox5.Visible = e
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Timer1.Enabled = False Then
                Counter = 0
                Timer1.Enabled = True
                Button1.Text = "Stop"
            Else
                Timer1.Enabled = False
                Button1.Text = "Start"
            End If
        End Sub
    End Class

     

     

    Friday, December 07, 2007 3:23 AM

All replies

  • This would be one way to do it . . .

     

    To run this code, create a form with a button and label on it.

     

    Code Block

    Public Class Form1

     

    Private Const BUTTONTEXT_PLAY As String = "Play"

    Private Const BUTTONTEXT_STOP As String = "Stop"

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Select Case Button1.Text

     

    Case BUTTONTEXT_PLAY

    'Do stop function here

    Button1.Text = BUTTONTEXT_STOP

    Label1.Text = "Playing"

     

    Case BUTTONTEXT_STOP

    'Do stop function here

    Button1.Text = BUTTONTEXT_PLAY

    Label1.Text = "Stopped"

    End Select

    End Sub

     

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Button1.Text = BUTTONTEXT_PLAY

    Label1.Text = ""

    End Sub

    End Class

     

     

     

    Friday, December 07, 2007 2:24 AM
  • That is what I had for a stand alone button, but I am looking for a way to exit a do loop or something similar when the button is pressed again.  This is the code I have assembled so far just to get it working.  I want to be able to exit the code block while the code is still running.  I know there is a way, I just cannot think of it.  The checkboxes (cbx1-5) are just blocks I am using to develop my own metronome with sound.  I can get it to work with a separate stop button, but that is also very slow in response.  Please help.

    Code Block

        Private Sub cmdMetronome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdMetronome.Click


            do until button is pressed again
                cbx(False, False, True, False, False)
                My.Computer.Audio.Play("C:\WINDOWS\Media\ding.wav")
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(False, False, False, True, False)
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(False, False, False, False, True)
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(False, False, False, True, False)
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(False, False, True, False, False)
                My.Computer.Audio.Play("C:\WINDOWS\Media\ding.wav")
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(False, True, False, False, False)
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(True, False, False, False, False)
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

                cbx(False, True, False, False, False)
                System.Threading.Thread.Sleep(((60 / tbxBPM.Text) / 5) * 1000)

            loop

            'Application.DoEvents()

        End Sub

        Private Sub cbx(ByVal a, ByVal b, ByVal c, ByVal d, ByVal e)

            cbx1.Visible = a
            cbx2.Visible = b
            cbx3.Visible = c
            cbx4.Visible = d
            cbx5.Visible = e

        End Sub
    Friday, December 07, 2007 2:59 AM
  •  

    A comment about what you doing - its very poor because you are using thread.sleep.    I cannot state this enough  USING THREAD.SLEEP IS A VERY BAD WAY TO CAUSE A DELAY IN AN APPLICATION.

     

    In you application for the vast majority of the time is spent in the sleep state meaning the UI will be completely dead and unresponsive to imput.    You should start thinking in terms of events.

     

    As stated numerous timers in this forums about delays/pauses/waits use of a timer is a much better approach and allows the UI to remain responsive.     A better approach using a counter and a timer is included which allows the process to be started and stopped at any time - the UI remains responsive throughout.   The speed of the UI change can be modified by simply modifying the interval property.

     

    Code Block

    Public Class Form1
        Dim Counter As Integer = 0


       
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Interval = 1000
            Button1.Text = "Start"

        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
           
            Select Case Counter Mod 4
                Case 0
                    'Play Sounds
                    My.Computer.Audio.Play("d:\WINDOWS\Media\ding.wav", AudioPlayMode.Background)
                    cbx(False, False, True, False, False)
                Case 1
                    cbx(False, False, False, True, False)
                Case 2
                    cbx(False, False, False, False, True)
                Case 3
                    cbx(False, False, False, True, False)
            End Select
            Counter += 1

        End Sub

     

        Private Sub cbx(ByVal a, ByVal b, ByVal c, ByVal d, ByVal e)
            ComboBox1.Visible = a
            ComboBox2.Visible = b
            ComboBox3.Visible = c
            ComboBox4.Visible = d
            ComboBox5.Visible = e
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Timer1.Enabled = False Then
                Counter = 0
                Timer1.Enabled = True
                Button1.Text = "Stop"
            Else
                Timer1.Enabled = False
                Button1.Text = "Start"
            End If
        End Sub
    End Class

     

     

    Friday, December 07, 2007 3:23 AM
  • I am sorry for my using of the sleep function.  That is the only thing that I knew how to use properly.  At the moment everything seems to be working the way I wanted.  Thanks for your help.
    Friday, December 07, 2007 4:07 AM
  • People use it because its a simple one line and usually because it avoids them having to learn the correct way which takes a bit more code and effort but results in a better functioning application.

     

    Friday, December 07, 2007 4:33 AM