Is it possible to use NotifyIcon without a form?

Hi,

I'm trying to write an application that sits in the system tray and runs in the background, displaying a balloon pop-up when a condition is met within the program.

Using Visual J# 2005 Express Edition, there's a form object called NotifyIcon, which would do what I need it to do, however, it is part of a form. Is there a way of using this class without having it attached to a form

If not is there any way I can achieve the same outcome I imagine I could use a blank form and hide it, though this doesn't seem like a very elegant method.

Any feedback would be appreciated.

Thanks

Luke


Answer this question

Is it possible to use NotifyIcon without a form?

  • Chris Mayo - MSFT

    Hi,

    I don't think there is any other alternative. You need to hide the parent form when icon is being shown. You may check this sample shipped with VS2005. It also does the same thing.

    Or you may post your query to one of the winforms forums here. It's the best palce for winforms related queries.

    Thanks.



  • Jose Rocco

    Hi,

    is it possible to use the notifyicon from a component without a form

    Amongin



  • _Prabhu Sadasivam

    Ok, thanks for your advice and quick reply.
  • Scott Stalter 1

    I bumped into this when searching for something else. The answer is that you don't need a form. Here is a quick example:

    Public Class TrayIconClass

    Private contextMenu1 As System.Windows.Forms.ContextMenu

    Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem

    Friend WithEvents notifyIcon1 As System.Windows.Forms.NotifyIcon

    Private components As System.ComponentModel.IContainer

    Public Sub Start()

    Init()

    While True

    Windows.Forms.Application.DoEvents()

    Threading.Thread.Sleep(20)

    End While

    End Sub

    Public Sub Init()

    components = New System.ComponentModel.Container

    contextMenu1 = New System.Windows.Forms.ContextMenu

    menuItem1 = New System.Windows.Forms.MenuItem

    ' Initialize contextMenu1

    Me.contextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() _

    {Me.menuItem1})

    ' Initialize menuItem1

    Me.menuItem1.Index = 0

    Me.menuItem1.Text = "E&xit"

    ' Create the NotifyIcon.

    Me.notifyIcon1 = New System.Windows.Forms.NotifyIcon(Me.components)

    ' The Icon property sets the icon that will appear

    ' in the systray for this application.

    notifyIcon1.Icon = New Icon(Application.StartupPath & "\visualcron.ico")

    ' The ContextMenu property sets the menu that will

    ' appear when the systray icon is right clicked.

    notifyIcon1.ContextMenu = Me.contextMenu1

    ' The Text property sets the text that will be displayed,

    ' in a tooltip, when the mouse hovers over the systray icon.

    notifyIcon1.Text = "VisualCron - TrayIcon"

    notifyIcon1.Visible = True

    End Sub 'New

    Private Sub notifyIcon1_DoubleClick(ByVal Sender As Object, ByVal e As EventArgs) Handles notifyIcon1.DoubleClick

    ' Show the form when the user double clicks on the notify icon.

    End Sub

    Private Sub menuItem1_Click(ByVal Sender As Object, ByVal e As EventArgs) Handles menuItem1.Click

    ' Close the form, which closes the application.

    End Sub

    End Class


  • Rep

    Hi,

    I didn't get fully what you mean by component. If you mean a user control then yes you can, but to show your own control, you will need a form to host that. So it's the same thing again.

    For more you can post your query on Winforms forum (link given in my last post).

    Thanks.



  • Is it possible to use NotifyIcon without a form?