How to make an Alarm clock with sound in C#
By FoxLearn 11/19/2024 9:20:20 AM 7.77K
You'll need a Timer
control to track the time, a DateTimePicker
to allow the user to set the alarm time, and a way to play a sound when the time is reached.
How to make an Alarm clock with sound in C#?
Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "AlarmClock" and then click OK
Drag and drop the DateTimePicker
, Button
, Label
, and Timer
controls from Visual Toolbox onto your form designer, then design your form as below
Add code to handle your form as below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AlarmClock { public partial class Form1 : Form { // Timer that will check if the current time matches the alarm time System.Timers.Timer timer; public Form1() { InitializeComponent(); } // Form Load Event - Initialize timer properties private void Form1_Load(object sender, EventArgs e) { // Set timer to tick every second timer = new System.Timers.Timer(); timer.Interval = 1000; // 1000 milliseconds = 1 second timer.Elapsed += Timer_Elapsed; } // Timer tick event private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //Update timer DateTime currentTime = DateTime.Now; DateTime userTime = dateTimePicker.Value; // Check if the current time matches the alarm time if (currentTime.Hour == userTime.Hour && currentTime.Minute == userTime.Minute && currentTime.Second == userTime.Second) { // Stop the timer once the alarm time is triggered timer.Stop(); UpdateLable upd = UpdateDataLable; if (lblStatus.InvokeRequired) Invoke(upd, lblStatus, "Stop"); // Play the alarm sound PlayAlarmSound(); } } //Update label control delegate void UpdateLable(Label lbl, string value); void UpdateDataLable(Label lbl,string value) { lbl.Text = value; } private void btnStart_Click(object sender, EventArgs e) { timer.Start(); lblStatus.Text = "Running..."; } private void btnStop_Click(object sender, EventArgs e) { timer.Stop(); lblStatus.Text = "Stop"; } } }
Create a play sound method like this:
// Method to play sound private void PlayAlarmSound() { // You can change this to any WAV file you want using (var player = new SoundPlayer("alarm.wav")) { player.PlayLooping(); // Plays sound continuously } }
Ensure you have a .wav
sound file (like alarm.wav
) in your project directory or provide the full path to the file. You can find royalty-free alarm sounds online or use any sound you prefer.
By default, the DateTimePicker
control allows setting both date and time. If you want only the time to be selected, set the Format
property to Time
and disable the date portion.
The System.Timers.Timer
class in C# triggers the Elapsed
event based on the interval defined by its Interval
property. This event can be handled to perform specific actions or tasks at the specified time intervals.
However, it's important to note that the System.Timers.Timer
has the same resolution as the system clock. If the Interval
value is smaller than the resolution of the system clock, the Elapsed
event may not fire as expected, as the system clock may not be able to handle such fine-grained intervals.
This simple alarm clock app will alert the user when the specified time is reached and play an alarm sound.
VIDEO TUTORIAL
- How to save files using SaveFileDialog in C#
- How to Display Images in DataGridView in C#
- How to Print DataGridView with Header & Footer with Landscape in C#
- How to Create a custom Progress Bar with Percentage in C#
- How to read an image file in C#
- How to use BackgroundWorker in C#
- How to protect .NET code from reverse engineering
- How to Get value from another Form in C#