Sleppatloh Miw
Member
- Joined
- Jan 31, 2021
- Messages
- 10
- Programming Experience
- Beginner
Hi there UAll,
New in using CSharp and learning step by step - also following some creations from YouTube.
So Here I am to put my question to you all.
First, here's my program - Clock Alarm:
When I start the program, no problem.
When the alarm goes on, I click stop, but the music remains playing.
I can put in my last lines from bttnStop_Click:
player.Stop();
However, when I try his, the program automatic tries to put SoundPlayer insted of player.
My question,
How can I stop also the music playing when I click on 'STOP'.
Kind regards and untill next time.
Lets Code!
Wim.
New in using CSharp and learning step by step - also following some creations from YouTube.
So Here I am to put my question to you all.
First, here's my program - Clock Alarm:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace ClockAlarm
{
public partial class Form1 : Form
{
System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += Timer_Elapsed;
}
delegate void UpdateLabel(Label lbl, string value);
void UpdateDataLabel(Label lbl, string value)
{
lbl.Text = value;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
DateTime currentTime = DateTime.Now;
DateTime userTime = dateTimePicker.Value;
if (currentTime.Hour == userTime.Hour && currentTime.Minute == userTime.Minute && currentTime.Second == userTime.Second)
{
timer.Stop();
try
{
UpdateLabel upd = UpdateDataLabel;
if (lblStatus.InvokeRequired)
Invoke(upd, lblStatus, "STOP");
SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Programmeren in C Sharp\Projecten\ClockAlarm\Electric Six - Danger! High Voltage.wav";
player.PlayLooping();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void bttnStart_Click(object sender, EventArgs e)
{
timer.Start();
lblStatus.Text = "Programma is lopende ...";
}
private void bttnStop_Click(object sender, EventArgs e)
{
timer.Stop();
lblStatus.Text = "Programma gestopt door de gebruiker.";
}
}
}
When the alarm goes on, I click stop, but the music remains playing.
I can put in my last lines from bttnStop_Click:
player.Stop();
However, when I try his, the program automatic tries to put SoundPlayer insted of player.
My question,
How can I stop also the music playing when I click on 'STOP'.
Kind regards and untill next time.
Lets Code!
Wim.
Last edited by a moderator: