Click here to Skip to main content
16,012,843 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Everyone

I am working on a project in C# wherein I am creating a calendar- reminder sort of.
I have coded the part where I am taking in as input the various dates to be remembered in an sql database.
What I want is.. the application should set an alarm and the particular event should pop up or show in a balloon from the notification tray using notifyicon.
C#
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip();

Also please can anyone tell me how to initialize only the notifyicon at startup (How to place its hex codes in regedit)instead of initializing the entire project at startup.

Thank You

[Edit]Code block added[/Edit]
Posted
Updated 20-Jun-13 23:22pm
v2
Comments
johannesnestler 21-Jun-13 5:29am    
Sorry, I don't really undestand what you are asking for. I understand:
you want to show a notify Icon while your application is running - some reminder is triggerd and your application should show a baloontip. So far so good. And it seems you have the code for it (you didn't say you have problems with showing the balloon tip..). And I think with "event" you mean a specific entry in your calendar app, don't you?
What I don't understand: What you mean with "initialize" at Startup (at application Startup? or System Startup?). Hex codes? regedit? (guess: you mean registry key?) entire Project? (guess: you mean your app?) But wait, that doesn't make sense - you can not run the "NotifyIcon" (which is part of your app) without your app running, right? So please clarify, I think I can help you reagarding (Auto-)startup and handling NotifyIcon tasks...
JPais 24-Jun-13 0:21am    
Sorry for the unclear question.
with the word 'event' I meant to say the entry in the calendar app.
Let me clarify it.
what I want is
My app takes in input of dates to be remembered (This part is done)
But when I ON the System I want the app to show the reminder if triggered (that is the event is on that particular day), the app should alert the user with an alarm and a balloon message using the notifyicon without the user starting the app separately.
I also have more doubts..
You said that NotifyIcon" cannot run without the app running, so..
1. If my app is able to trigger the reminder, will my app be running in the background?? (Because I thought only the notifyicon can be initialized at System startup and the app is not required. Sorry for this.. I am using the notifyicon for the first time)
2. Does it use up a lot of memory??

Thank You for your help.
johannesnestler 24-Jun-13 3:39am    
Hi JParis,

Ok, so I interpreted your "missunderstanding" right... Your app has to run in the background - how should it handle the alerts if not running? The NotifyIcon can not run independend from it's hosting app. Initialization at System Startup? No - it's initialized as every other Component/Control of your Windows App. Memory consumption for a NotifyIcon? No problem for shure. So what can you do? -> see my upcoming solution...
JPais 25-Jun-13 4:41am    
Thanks for the help.
But I am finding an error with the program.cs file.
Is it because it is declared in the Form itself as 'static class program'?
I am getting an error

The namespace 'NotifyIcon_AutoStartSample' already contains a definition for 'Program'
johannesnestler 25-Jun-13 6:09am    
? Must be a mistake while copy on your side. Start with a forms project named NotifyIcon_AutoStartSample. Replace Program.cs with my Code. Run...edit: don't forget to add an icon!

1 solution

Hi again JParis,

... So what can you do?

Now we can talk about (Windows-)Services and so on, but I think easiest for you is just to start your app when the Computer starts (Autostart, registry entry) and let it run in the background (only visible for the user through the NotifyIcon).

Just create one app - let one "part" trigger the reminders (watch "events") and the other "part" showing the reminders (BaloonTips).

Use the NotifyIcon instead of the taskbar if your app is minimized.
Autostart your app when Windows starts (could use "AutoStart" or setting the "Run" registry key for your app.

Maybe you want to play arround with the following (runnable) example showing a typical NotifyIcon resisizing and minimizing szenario. I also added some code to read and write the registry "Autostart" entry - You must have rights to access registry (best run as admin)! Otherwise writing to the registry will fail! So whats left is to add your own Icon for the NotifyIcon instead of the one I used to test the example project.

using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace NotifyIcon_AutoStartSample
{
    static class Program
    {
        class SampleForm : Form
        {
            NotifyIcon m_notifyicon;
            
            ContextMenuStrip m_ctxmenuNotify;            
            ToolStripMenuItem m_miNotifyExit;
            ToolStripMenuItem m_miNotifyRestore;

            CheckBox m_cbShowInTaskbarWhenMinimized; // in your real app you have to persist this setting (ApplicationSettings?).
            CheckBox m_cbAutoRunWithWindows;

            public SampleForm()
            {
                // Create Menu for NotifyIcon
                m_ctxmenuNotify = new ContextMenuStrip();
                m_ctxmenuNotify.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuNotifyOpening);
                m_miNotifyExit = new ToolStripMenuItem("Exit");
                m_miNotifyExit.Click += new EventHandler(MenuNotifyExitOnClick);
                m_miNotifyRestore = new ToolStripMenuItem("Restore");
                m_miNotifyRestore.Click += new EventHandler(MenuNotifyRestoreOnClick);
                m_ctxmenuNotify.Items.Add(m_miNotifyExit);
                m_ctxmenuNotify.Items.Add(m_miNotifyRestore);

                // Create a typical menu for a NotifyIcon
                m_notifyicon = new NotifyIcon();
                m_notifyicon.Icon = NotifyIcon_AutoStartSample.Properties.Resources.Icon1; // Replace with your own icon!
                m_notifyicon.Text = Text;
                m_notifyicon.Visible = true;
                m_notifyicon.Click += new EventHandler(NotifyIconOnClick);
                m_notifyicon.ContextMenuStrip = m_ctxmenuNotify;

                // A CheckBox for selecting if app should be shown in the taskbar
                m_cbShowInTaskbarWhenMinimized = new CheckBox();
                m_cbShowInTaskbarWhenMinimized.Text = "Show in Taskbar when minimized";
                m_cbShowInTaskbarWhenMinimized.Dock = DockStyle.Top;

                 // A CheckBox for auto run
                m_cbAutoRunWithWindows = new CheckBox();
                m_cbAutoRunWithWindows.Text = "AutoStart with windows";
                m_cbAutoRunWithWindows.Dock = DockStyle.Top; 
                m_cbAutoRunWithWindows.Checked = IsAutoRunWithWindowsEnabled();
                m_cbAutoRunWithWindows.CheckedChanged += new EventHandler(m_cbAutoRunWithWindows_CheckedChanged);
                
                Controls.Add(m_cbShowInTaskbarWhenMinimized);
                Controls.Add(m_cbAutoRunWithWindows);
            }

            void ContextMenuNotifyOpening(object obj, System.ComponentModel.CancelEventArgs cea)
            {
                if (WindowState == FormWindowState.Minimized)
                    m_miNotifyRestore.Text = "Restore";
                else
                    m_miNotifyRestore.Text = "Minimize";
            }

            void MenuNotifyRestoreOnClick(object obj, EventArgs ea)
            {
                if (WindowState == FormWindowState.Minimized)
                {
                    Visible = true;
                    WindowState = FormWindowState.Normal;
                    Activate();
                }
                else
                {
                    WindowState = FormWindowState.Minimized;
                }
            }

            void NotifyIconOnClick(object obj, EventArgs ea)
            {
                MouseEventArgs mea = ea as MouseEventArgs;

                if (mea.Button != MouseButtons.Left)
                    return;

                if (WindowState == FormWindowState.Minimized)
                {
                    Visible = true;
                    WindowState = FormWindowState.Normal;
                }
                Activate();
            }

            void MenuNotifyExitOnClick(object obj, EventArgs ea)
            {
                Close();
            }

            protected override void OnResize(EventArgs ea)
            {
                if (!m_cbShowInTaskbarWhenMinimized.Checked)
                {
                    if (WindowState == FormWindowState.Minimized)
                        Visible = false;
                }

                base.OnResize(ea);
            }

            protected override void OnClosed(EventArgs ea)
            {
                base.OnClosed(ea);

                // Dispose NotifyIcon if this Form closes. (not needed if you add your NotifyIcon with the FormsDesigner)
                if (m_notifyicon != null)
                    m_notifyicon.Dispose();
            }            

            void m_cbAutoRunWithWindows_CheckedChanged(object sender, EventArgs e)
            {
                SetAutoRunWithWithWindows(m_cbAutoRunWithWindows.Checked, String.Empty);
            }

            static public bool SetAutoRunWithWithWindows(bool bEnable, string strArgs)
            {
                RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (regkey == null)
                    return false;

                string strPath = Application.ExecutablePath + (String.IsNullOrEmpty(strArgs) ? String.Empty : " " + strArgs);
                if (bEnable)
                {
                    regkey.SetValue(Path.GetFileNameWithoutExtension(Application.ExecutablePath), strPath, RegistryValueKind.String);
                }
                else
                {
                    regkey.DeleteValue(Path.GetFileNameWithoutExtension(Application.ExecutablePath), false);
                }

                return true;
            }

            static public bool IsAutoRunWithWindowsEnabled()
            {
                RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (regkey == null)
                    return false;

                if (!String.IsNullOrEmpty((string)regkey.GetValue(Path.GetFileNameWithoutExtension(Application.ExecutablePath))))
                    return true;

                return false;
            }

        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new SampleForm());
        }

    }
}


I hope this helps you in deciding what to do for your app - feel free to ask if you have any further questions.

Kind regards Johannes
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900