I am coding something fairly simple and I've run into a few barriers. Keep in mind I don't have much experience in VB.NET.

1) I have created timer that is activated by the user with a user-defined time. But, when the "Start" button is clicked, the timer immedieatly sets off the alarm. Is there any way to prevent this from happening?

2) Still related to the timer, it sets off two things, the alarm and a MsgBox, the problem here is that when the MsgBox is closed I need the alarm to shut off too. How would I go about doing that?

3) On to the alarm, I need to create a checkbox that when check will allow the timer to activate the alarm and if not disallow.

Any and all help is appreciated!

Start button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Start()
        TimerState.Text = "Active"

        If IntervalBox.Text = "" Then
            Timer1.Stop()
            TimerState.Text = "Disabled"
            MsgBox("Please enter an interval.")
        End If

        If MessageBox.Text = "" Then
            Timer1.Stop()
            TimerState.Text = "Disabled"
            MsgBox("Please enter a reminder message.")
        End If

        If IntervalBox.Text <= 5000 Then
            My.Computer.Audio.Stop()
            Timer1.Stop()
            MsgBox("Please enter an interval larger than 5000 to prevent spam.")
            TimerState.Text = "Disabled"

        End If
    End Sub

Timer:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Interval = IntervalBox.Text
        My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
        MsgBox(MessageBox.Text)
        'My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
    End Sub

The button click event should only start/stop (toggle) the timer. The timer tick handler is where you do the decrementing, testing and end-processing. If the timer interval is set to 1000 then it will "tick" once a second. The processing in the tick handler would be

decrement the countdown value
update the displayed value

if countdown value is zero then
    timer1.Disabled = True
    My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
    MsgBox(MessageBox.Text)
    TimerState.Text = "Expired"
end if

@Reverend Jim

You'll have to expand on that, like I said, I have almost no experience in VB.NET or coding in general.

I fixed problem #2 by adding "My.Computer.Audio.Stop()" under the code that executes the MsgBox.

Edited 4 Years Ago by Dv9UnKnoWn

I would define a class level variable to hold the countdown value.

Private countdown As Integer

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

    Dim btn As Button = sender

    If btn.Text = "Start Timer" Then
        countdown = 100
        btn.Text = "Stop Timer"
        TimerState.Text = "Active"
        Timer1.Enabled = True
    Else
        btn.Text = "Stop Timer"
        TimerState.Text = "Inactive"
        Timer1.Enabled = False
    End If

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    countdown -= 1
    txtTimer.Text = timeout

    If countdown = 0 Then
        timer1.Enabled = False
        My.Computer.Audio.Play(My.Resources.alarm)
        MsgBox(MessageBox.Text)
        TimerState.Text = "Expired"
    End If

End Sub    

Alright, I got that working. How do I make a check box enable/disable the alarm that is played by the timer?

You put a checkbox on the form and in the timer event you do

If cbxAudibleAlarm.Checked Then
    My.Computer.Audio.Play(My.Resources.alarm)
End If

That doesn't work.

"'System.IO.UnmanagedMemoryStream' cannot be converted to 'String'."

If

My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
MsgBox(MessageBox.Text)
My.Computer.Audio.Stop()

works then

If cbxAudibleAlarm.Checked Then
    My.Computer.Audio.Play(My.Resources.alarm, AudioPlayMode.BackgroundLoop)
End If

MsgBox(MessageBox.Text)

If cbxAudibleAlarm.Checked Then
    My.Computer.Audio.Stop()
End If    

should also work

This question has already been answered. Start a new discussion instead.