[RESOLVED] Create a formless app?
I want to create an application that has/needs no form or controls. When launched, it would show an icon in the tray area & run some code at certain intervals using a timer control. Can someone explain how to make an app with no form, as I have never done that before. I can handle the timer & notify icon part, I just need help with the form (or lack of). Thanks...
Re: Create a formless app?
Just have your startup object as a Module with Sub Main.
Re: Create a formless app?
Here is a little example of RobDogg's solution to get you started.
http://www.codeproject.com/useritems/NotifyChecker.asp
Good luck. :bigyello:
Re: Create a formless app?
It is pretty easy, you just have to account for some things that generally are taken for granted because the windows form does it for you.
You need to manually create everything, manually hook up event handlers, and manually dispose of the objects when the app closes. The trick is to also call Application.Run() to create a message loop. Otherwise the exe would run and close right away.
Here are the steps:
1) Create a new Windows Forms application, and delete the default Form1 that is added by Visual Studio
2) Add a module to the project
3) Paste code from below into module
4) Go into project properties and uncheck "Enable Appliction Framework" and change "Startup Object" to "Sub Main"
5) Add an icon to the resources section of your project properties (the one I used in my example code was called inet.ico, just change the code to match your icon name
Code:
Module Module1
Private MyNotifyIcon As NotifyIcon 'ICON IN SYSTEM TRAY COMPONENT
Private MyContextMenu As ContextMenu 'CONTEXT MENU WHEN ITEM IS CLICKED ON
Private MyTimer As Timer
Public Sub Main()
'CREATE INSTANCES OF OBJECTS WE NEED
MyNotifyIcon = New NotifyIcon
MyContextMenu = New ContextMenu
MyTimer = New Timer
MyNotifyIcon.Icon = My.Resources.inet 'SET AN ICON (ICON IS A RESOURCE IN THE PROJECT)
MyNotifyIcon.Visible = True 'SET VISIBLE
'ADD CAPTIONS AND EVENT HANDLERS FOR ITEMS IN CONTEXT MENU
MyContextMenu.MenuItems.Add("Show Messagebox", AddressOf ShowMessagebox)
MyContextMenu.MenuItems.Add("Exit", AddressOf ExitApplication)
MyNotifyIcon.ContextMenu = MyContextMenu 'ASSIGN CONTEXT MENU TO ICON
'SETUP TIMER
MyTimer = New Timer
MyTimer.Interval = 10000 '10 seconds
MyTimer.Start()
AddHandler MyTimer.Tick, AddressOf Timer_Tick
'CREATE A MESSAGE LOOP WITH NO FORM, OTHERWISE THE APPLICATION WOULD EXIT RIGHT AWAY
Application.Run()
End Sub
'DELEGATE SUB TO EXIT APPLICATION
Private Sub ExitApplication(ByVal sender As Object, ByVal e As EventArgs)
'CLEAN UP YOUR OBJECTS
MyTimer.Stop()
MyTimer.Dispose()
MyContextMenu.Dispose()
MyNotifyIcon.Dispose()
'EXIT APPLICATION LOOP
Application.Exit()
End Sub
'DELEGATE SUB TO SHOW MESSAGEBOX
Private Sub ShowMessagebox(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Hello World")
End Sub
'DELEGATE TO HANDLE TIMER TICKING
Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
MyTimer.Stop()
MessageBox.Show("Timer Ticked")
MyTimer.Start()
End Sub
End Module
Re: Create a formless app?
Unless you really need to have the tray icon or need to interact with the current user in other ways, you might want to consider writing this app as a Windows Service. This would insure that it would always be running as long as the computer was turned on. As a regular app, you would have to depend on someone being logged in for your app to run.
Re: Create a formless app?
Thanks guys. I will try your suggestions...
Re: Create a formless app?
I have ran across another method to make a formless app. It involves setting the WindowState property to Minimized & the ShowInTaskbar property to False. So, essentially, there is a form, but it is never shown. Is this an acceptable way to go "formless", or are the methods mentioned above the better way to do it?
Re: Create a formless app?
you are making unused and unneeded resources by doing it that way.
It will work, and it could make your life a bit easier because you can use the form to add components like timers and use the GUI of Visual Studio to setup properties and such, but I still would recommend the method I posted above, as it only creates resources for exactly what you need.
Re: Create a formless app?
You can still use timers and stuff without forms.
Re: Create a formless app?
kleinma ... is there an event to capture when I click on a NotifyIcon BalloonTip? Thanks...
Re: Create a formless app?
Re: Create a formless app?
Quote:
Originally Posted by nbrege
kleinma ... is there an event to capture when I click on a NotifyIcon BalloonTip? Thanks...
yes you just need to hook it up to a subroutine manually. If you look at my sample code above, I do that very thing with the timer and its tick event.