Locate Notify Icon Tool from the Toolbox. If you can't find it on toolbox Right-Click anywhere on the toolbox then select Choose Items. A dialog will appear, under .Net Framework Components tab check Notify Icon then ok. your done.
Drag the Notify Icon tool to your form.
Goto to code page:
declare an icon i named mine as e_icon
Private e_icon As Icon
under form load event type the following code:
e_icon = New Icon(My.Application.Info.DirectoryPath + "\erlingshi.ico")
Me.NotifyIcon1.Icon = e_icon
Me.Icon = e_icon
Your code should look like this:
The My.Application.Info.DirectoryPath is your applications directory path. Your designed icon file should be located on your application path. For example my applications directory path is c:\sample and my icon filename is erlingshi.ico then the code My.Application.Info.DirectoryPath + "\erlingshi.ico" output should be c:\sample\erlingshi.ico.
Run your application. The application should run perfectly.
Now iwant to add some menu on my notify icon.
Go to your project switch to designer view. On your toolbox locate Context Menu Strip.
Drag it to your form. Add menu items. Open Form and Close. Your Form should look like this.
Now select your Notify Icon. Under Properties Toolbox change ContextMenuStrip Property to the one you just created. mine is ContextMenuStrip1.
Run your application.
Right-Click your notification icon on your task bar a menu will appear.
Great!
Now the next thing i want to do is when i close my form the program must still be running. To be more simple i'll just hide the form when trying to close it.
Go to your project and switch to Code View.
Under Form_Closing event type the following code:
e.Cancel = True
Me.Hide()

e.Cancel = True will prevent your form from being closed.
Me.Hide() will hide your form.
Now let's use our menu item from the Notify Icon we created before.
Go to your project, switch to Design View then click on the ContextMenuStrip1 we created to show Menu Items on your form.
Double-Click on Open Form menu item to jump to its Click event code view. Type this code to show the form:
Me.Show()
Now again switch to Design View then click on the ContextMenuStrip1.
Double-Click on Close menu item to jump to its Click event code view. Type this code to end your application:
End
Your Code should look like this:

Run your Program to see the effect.
Now the last thing i want to share is how to show a balloon tip on your Notify Icon.
We have 3 kinds of Balloon Tip icon
1. Information Icon
2. Warning Icon
3. Error Icon
or None
It's up to you how your going to use this balloon Tip but here is the code.
NotifyIcon1.ShowBalloonTip(time out in milliseconds, title, text, icon)
sample:
1. Information Icon
NotifyIcon1.ShowBalloonTip(5000, "Sample!", "New Updates Found!", ToolTipIcon.Info)

2. Warning Icon
NotifyIcon1.ShowBalloonTip(5000, "Warning!", "Virus Detected!" + vbNewLine + "Location: C:\WINDOWS\system32\" + vbNewLine + "Name: Emil Virus", ToolTipIcon.Warning)

3. Error Icon
NotifyIcon1.ShowBalloonTip(5000, "Error!", "System File is missing!", ToolTipIcon.Error)

We have lots of customized Balloon Tip/Notify Icons to use in vb .net but this is the easiest one i know...hehe
THAT'S ALL FOLKS! THANK YOU!
No comments:
Post a Comment