Results 1 to 12 of 12

Thread: [RESOLVED] Create a formless app?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [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...

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Create a formless app?

    Just have your startup object as a Module with Sub Main.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    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.

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  5. #5
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Create a formless app?

    Thanks guys. I will try your suggestions...

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    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?

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  9. #9
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Create a formless app?

    You can still use timers and stuff without forms.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Create a formless app?

    kleinma ... is there an event to capture when I click on a NotifyIcon BalloonTip? Thanks...

  11. #11
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Create a formless app?


  12. #12
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width