Simple VB.NET Notify Icon with Panel Application - Let's Get Cookin'!
(Page 5 of 6 )
We’ll now switch our attention to the coding aspect, where we will apply the programming logic. Select the form and double click it, the code in the “code view” will provide you with the Form_Load event handler and enter the code as shown below.
Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
nfi.Visible = False 'hide the icon when the form loads
Panel1.Visible = True ' show only Panel1 during the start
Panel2.Visible = False 'hide the panel2
End sub
Double click the button labeled Panel1; the code below will be displayed in the code view:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
We want the above sub-routine to be executed when pan1 (context menu item) is clicked, so add the following click event to the above code and it goes as:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,pan1.click
Hence this sub will be triggered either when the button is clicked or when the Notify Icon menu item Panel1 (pan1) is clicked.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,pan1.click
Me. Visible = True 'make sure that the form is displayed first if it's been triggered from notify icon
Panel1.Visible = True ' activate the first panel
Panel2.Visible = False ' hide the second panel
End Sub
Do the same for the Button2_click event handler. In the code view there will be two drop-down boxes with the components name in them. The left-hand drop-down box lists the components in the current form and the right-hand drop-down box lists the methods of selected components.
From the left-hand box select the LinkLabel1 control. From the right-hand select the click event. You will be provided with the link label click event handler which we’ll code here:
Private Sub LinkLabel1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkLabel1.Click
Me.Visible = False ' hide the form
nfi.Visible = True ' show the notify icon in the system tray
End Sub
We want the application to be visible when the Iotify Icon is double clicked, so select the notify icon (nfi) from the drop down box in the code view and select the double click event from the associated events:
Private Sub nfi_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles nfi.DoubleClick
Me.Visible = True
nfi.Visible = False
End Sub
Select the exit menu item and double click it.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
Me.close()
End Sub
Bingo! We are done now. Build the application and run it. The complete run of the code follows next:
Next: The Code >>
More VB.Net Articles
More By R. Abhiram Vikrant