Jump to content

Creation icons in the Notification bar - VB.NET

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
14 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
In your Notification bar you can found a lot of icons from different programs, some for just letting you open the program fast(by clicking or double-clicking the icon) while some has more functions in a menu(by right clicking the icon) but most programs with the icons have both. I will in this tutorial show how to add a icon like this and also how to add a menu to it.




First we need to create an NotifyIcon, do that by dragging it from the toolbox and then name it exampleIcon. Change Text to "This is my Icon". We also need to set which icon it should use, if you want another one then your program has then you can choose it, otherwise we can use some code to copy the one that is used by the program itself.


[highlight=VB.NET] Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
exampleIcon.Icon = Me.Icon
End Sub[/highlight]


As simple as that. So what it does is when the forms load we copy the forms icon to the NotifyIcon's icon. So now if you test your application an icon will pop up in the Notification Bar with the text "This is my Icon" and with the same icon as your program. But if you closes the application the icon still remains, this is because it don't updates the icons all the time, move your mouse over the icon and it will now update in realize it should be there and therefor disappear. To solve this problem we could add some more code to the program which makes the icon disappear when the form is closed.


[highlight=VB.NET] Private Sub frmMain_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
exampleIcon.Dispose()
End Sub[/highlight]

So when the form is disposed we also dispose the icon.



But our icon doesn't currently do anything at all more then just "being there". We should add some code to make it show the form if we double clicks on the icon, to do this we need to do two things, we need to unminimize the form if it is minimized so the user can see it and also Activate it won't be covered by other programs. But when unminimizing the form it will automatically be Activated so we only need to Activate the form if it isn't minimized.




[highlight=VB.NET] Private Sub exampleIcon_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles exampleIcon.MouseDoubleClick
If Me.WindowState = FormWindowState.Minimized Then
Me.WindowState = FormWindowState.Normal
Else
Me.Activate()
End If
End Sub[/highlight]








BalloonTips


We can also add something that is called BalloonTip, it's the bubbles that are showed from the icon sometimes. Remember that these things could be annoying for the user if done to much so think it through before using them. But I'll show how to do it of course. We will need to go back to the property window for the NotifyIcon to add the BalloonTip. Change the BalloonTipIcon to Info the BalloonTipText to "This could be annoying" and BalloonTipTitle to "Information". Now we've created the BalloonTip and only need to make it show. We need to use ShowBalloonTip to show the Tip. We now need to write how many milliseconds this Tip should be showed, to show how annoying it could be we adds it to the Load event of the form.



[highlight=VB.NET] Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
exampleIcon.Icon = Me.Icon
exampleIcon.ShowBalloonTip("3000")
End Sub[/highlight]


Note that even though I'm pointing out it could be annoying it don't have to be that if you use it in the right way, I just want to warn you from using it too much in the wrong situations.










ContexMenuStrips


Now there's time to add a menu, the menus is called ContexMenuStrips. Drag one from the toolbox onto your form and name it exampleStrip, when having the ContexMenuStrip selected we will see some boxes in top of the visual form. Here you can just type some text to make items to the menu. If you want to have a little more control you can go to properties and then edit Items, here you will have all options for the items. Now just create some items. If you want things to happen when you click on a menu you just add a click event to that one with the desired code.


Now go back to the NotifyIcon and set the ContexMenuStrip property to the ContexMenuStrip(exampleStrip). Then it's time to test it, run your program and right click your icon, now the menu will appear. So that was mostly it, now you can create a NotifyIcon and use it to make your program easier to use. I hope you liked this one, Cya :)

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
This is very handy to know. +rep!

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts

Jordan said:

This is very handy to know. +rep!

Thanks :) But haven't received any +rep :P

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Nice +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Tareq

Tareq

    Newbie

  • Members
  • Pip
  • 1 posts
Hi,

Is there any way to make a context menu item default in the system tray of my own application ?

#6
Egz0N

Egz0N

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,034 posts
nice one .. i like it :) .. +rep

#7
Bioshox

Bioshox

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
this helped alot in my college work/revision, thanks!!

#8
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
When I put in
 exampleIcon.Icon = Me.Icon
and run the program nothing pops up am I doing something wrong?
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#9
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You have to create the notify icon first, read the beginning of the second paragraph.

#10
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
I did but it still doesn't do any thing.:confused:
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.

#11
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
What version of VB are you using?

#12
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
Microsoft Visual Basic 2008 Express Edition.
A man can be defined by what he does when no one is looking.
Science is only an educated theory, which we cannot disprove.