Introduction
I usually forget about my home work, my exams, my jobs :/. So I decided to write an application to remind me of these. Almost all of you use your computers for hours every day. The computer is harmful for all of our brains if used for a long time. With this application, we won't forget our to-dos. I will also explain parts of the code for this application.
Features
- When you save a note, Reminder v1.0 saves it to File->Show Note ToolStripMenu for easy access. On every startup of Reminder v1.0, you can see these notes like in Office Word.
- You can use your saved notes or write a new note for a
TIMEDWARN
. It chooses a note for you. TIMEDWARN
is a warning message for which you can set the time and date.
- Warning with sound or without sound.
- You can choose Windows sounds for warning or custom songs (.mp3).
Here is a list of WarnSounds:
- When you hover over Windows sounds or custom songs with mouse, you will be able to listen to them. This is implemented with
Sound.Play()
or the MCI API.
With Sound.PLay()
:
With MCI Play command string:
- And the app is now available with a Turkish or English interface.
Using the code
My main Form1.cs is formed of these parts:
private void SaveNoteToMenu(string text)
{
newnote = new ToolStripMenuItem(text);
newnote.Text = text;
newnote.Click += new EventHandler(Item_Click);
newnote.CheckedChanged += new EventHandler(Checked_Changed);
newnote.CheckOnClick = true;
showNoteToolStripMenuItem.DropDown.Items.Add(newnote);
}
private void TakeNotesToMenu()
{
string[] Files = Directory.GetFiles(SavePath);
foreach (string file in Files)
{
FileInfo fileInfo = new FileInfo(file);
if (fileInfo.Extension==".note")
SaveNoteToMenu(fileInfo.Name);
}
}
Check IsRTBStable
: This function helps you to control RTB. I have four kinds of warnings to the user if something goes wrong. These are:
string FirstRTBTEXT = "Write Your Note ..";
string NameWarn = "First Give A Name Yo Your Note...";
string notewarn = "FIRST WRITE SOMETHING";
string NoNote = "There is No Note To Show";
Then I should control the RTB so its text should not be these warnings.
private bool IsRichTextBoxStable()
{
if (richNote.Text != FirstRTBTEXT
&& richNote.Text != null && richNote.Text != notewarn
&& richNote.Text!=NameWarn && richNote.Text!=NoNote)
return true;
else
return false;
}
Form functions
On Form load, I check if the time is WarnTime
. Is there a TIMEDWARN
? Are notes Warned
? Are there custom sounds? Get Windows sounds. I use four main if
statements for these.
Component functions
Here is the ToolStripMenuItem
's Click
event:
I should check on click of the CustomSong menu or on ShowNote menu or Warn Sounds menu, but Sounds menu would be different from Notes menu. We can check this with the sender
object and a few if
statements.
private void Item_Click(object sender, EventArgs e)
{
if (sender is ToolStripMenuItem) {
foreach (ToolStripMenuItem item in
(((ToolStripMenuItem)sender).GetCurrentParent().Items))
{
if (item == sender)
{
item.Checked = true;
clicked = true;
if (sender.ToString().Contains(".note"))
{
txtNoteName.Text = item.Text;
richNote.ResetText();
richNote.Text = functions.ShowNote(SavePath + "//" +
txtNoteName.Text);
}
else if (sender.ToString().Contains(".wav"))
{
WarnSoundWAV = sender.ToString();
soundselected = true;
CustomSoundSelected = false;
functions.PlayStopSound(sender.ToString(), false);
foreach (ToolStripMenuItem snd in
listToolStripMenuItem.DropDown.Items)
snd.Checked = false;
}
else {
WarnCustomSound = sender.ToString();
CustomSoundSelected = true;
soundselected = false;
functions.StopSound();
foreach (ToolStripMenuItem snd in
warnSoundsToolStripMenuItem.DropDown.Items)
snd.Checked = false;
}
}
if ((item != null) && (item != sender))
item.Checked = false;
}
}
}
The OtherFunctions.cs class is formed of four parts:
String.Format("{0:t}", dt); String.Format("{0:d}", dt); String.Format("{0:T}", dt); String.Format("{0:D}", dt); String.Format("{0:f}", dt); String.Format("{0:F}", dt); String.Format("{0:g}", dt); String.Format("{0:G}", dt); String.Format("{0:m}", dt); String.Format("{0:y}", dt); String.Format("{0:r}", dt); String.Format("{0:s}", dt); String.Format("{0:u}", dt);
public string GetDateTime(string DateOrClock)
{
DateTime dt = DateTime.Now;
if (DateOrClock == "Date")
return String.Format("{0:D}", dt); else if (DateOrClock == "Clock")
return String.Format("{0:T}", dt); else if (DateOrClock == "DateAndClock")
return String.Format("{0:f}", dt);
else
return "";
}
Note functions:
StreamWriter
and StreamReader
help us write or read from a stream like text files. For more info, please refer to MSDN: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx.
public void SaveNote(string path, RichTextBox rtb)
{
StreamWriter sw = new StreamWriter(path);
for (int i = 0; i < rtb.Lines.Length; i++)
sw.WriteLine(rtb.Lines[i]);
sw.Close();
}
public string ShowNote(string path)
{
string Note;
StreamReader sr = new StreamReader(path);
Note = sr.ReadToEnd();
sr.Close();
return Note;
}
Warning functions:
SetStartup
: Windows keeps all startup applications in Regedit "CurrentUser\SOFTWARE\Microsoft\Windows\CurrentVersion\Run". So we should set this with our application name and /autoRun command line.
We use Checkstartup
to check our OnStartup ToolStripMenuItem
when application starts on form load event.
public void SetStartup(bool start)
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey(
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
RegistryKey rkk =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Reminder v1.0\\Note", true);
if (start)
{
rk.SetValue(Path.GetFileName(Application.ExecutablePath).Replace(".exe", ""),
Application.ExecutablePath.ToString() + " /autoRun");
rkk.SetValue("CheckStartup", 1);
}
else
{
rk.DeleteValue(Path.GetFileName(
Application.ExecutablePath).Replace(".exe", ""), false);
rkk.SetValue("CheckStartup", 0);
}
}
IsWarnTime
: When user sets a TIMEDWARN
, Reminder v1.0 writes a note to Registry, then with a timer, every second it should control the warnings. Let's get the Registry keys.
public bool IsWarnTime()
{
RegistryKey rk =
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Reminder v1.0\\Note", true);
DateTime dt = DateTime.Now;
string[] DateParts = rk.GetValue("TIME").ToString().Split('.');
if (dt.ToLongDateString() ==
DateParts[0] && dt.ToLongTimeString() == DateParts[1] +
":" + DateParts[2] + ":" + DateParts[3])
{
rk.SetValue("IsWarned", 1);
return true;
}
else
return false;
}
Get Windows sounds:
This is very easy. Windows keeps sounds in "C:\Windows\Media". We will just get them :)
public ArrayList GetWindowsSounds()
{
string[] AllFiles = Directory.GetFiles("C:\\Windows\\Media");
ArrayList Sounds = new ArrayList();
foreach (string file in AllFiles)
{
FileInfo fileInfo = new FileInfo(file);
if (fileInfo.Extension == ".wav")
Sounds.Add(fileInfo.FullName);
}
return Sounds;
}
Play or stop these sounds: The SoundPlayer
class is used to play ".wav" files. It just plays Wav files and uses the System.Media
reference.
public void PlayStopSound(string SoundPath,bool PlayOrStop)
{
SoundPlayer Sound = new SoundPlayer(SoundPath);
Sound.Load();
if (PlayOrStop)
Sound.PlayLooping();
else
Sound.Stop();
}
Use MCI to play songs: If you really want to learn about MCI, see my other article, or start from this website: http://msdn.microsoft.com/en-us/library/aa733658(v=vs.60).aspx.
public void OpenFileToPlay(string filename)
{
string Pcommand = "open \"" + filename+ "\" type mpegvideo alias MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
Play(true);
}
public void Play(bool loop)
{
string Pcommand = "play MediaFile";
if (loop)
Pcommand += " REPEAT";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
}
public void StopSound()
{
string Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
} [DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn,
int iReturnLength, IntPtr hwndCallback);
And that's all. This is very simple way to implement a reminder. You can improve this small application in many ways. Don't forget the people who you love and who love you.
History
I am new at writing articles. :) Please support me to in doing this because I like it. And I believe I can do it better on every article. Thanks. Happy coding :)