PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Change Exe Icon Dynamically - "Me is not valid within a module"-VBForums
Results 1 to 13 of 13

Thread: Change Exe Icon Dynamically - "Me is not valid within a module"

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    5

    Change Exe Icon Dynamically - "Me is not valid within a module"

    I am trying to change executable icon programmatically of Console App (.NET Framework), but getting an error "Me is not valid within a module"

    Below is the code, please help.

    Module Module1
    Sub Main()
    Me.Icon = My.Resources.RighHandIcon
    End Sub
    End Module

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,658

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    The Me keyword refers to the current instance of the type it is in. That means that Me is only valid in an instance member. Using Me in a Shared member of a class is not valid because there is no current instance. Every member of a module is like a Shared member of a class because modules can't be instantiated, so Me is never valid in a module.

    Apart from that, the module is not the application so even if the module could use Me, that would not be referring to the application itself so you couldn't change the application icon that way. Changing the application icon would require changing the binary code of the compiled EXE, which is not something you could ever do simply by setting a property of an object.

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    55

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Me only works for Forms and Classes not Modules.

    It means the current Form or Class when you use Me so you need to do that code in a Module or use like Form1.Icon = Blah blah instead

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,658

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by pkedpker View Post
    Me only works for Forms and Classes not Modules.
    For the record, forms are classes. Also, Me is still not valid in a class if it's used in a Shared member.
    Quote Originally Posted by pkedpker View Post
    It means the current Form or Class when you use Me so you need to do that code in a Module or use like Form1.Icon = Blah blah instead
    The question does say the "Exe Icon", which I took to mean the icon displayed in Windows Explorer for the application. That's what "Exe Icon" should mean. You've interpreted it to mean the icon for a form, i.e. the icon displayed in the title bar of that form. Each form can have a different icon so none of them are the icon for the application but it may well be that the OP actually meant the icon for the main form. In that case, it would make more sense to put the code in the Load event handler of that form, in which case Me would make sense.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,658

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    OK, I just noticed that the OP says "Console App", which I missed before. In that case there are no forms and the "Exe Icon" definitely does mean what I thought. In that case, changing the application icon doesn't make sense as a concept. You can embed multiple icons in an executable and the user can then select which one they want for a shortcut but the icon for an executable file is determined at compilation. Even if you could use Me, there is no Icon property to set. That code was presumably copied from a WinForms app, where it would have been setting the icon for the current form only.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    5

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by jmcilhinney View Post
    The question does say the "Exe Icon", which I took to mean the icon displayed in Windows Explorer for the application. That's what "Exe Icon" should mean.
    That is right, by "Exe Icon" I mean Icon displayed in Windows Explorer for the application, not the form Icon.

  7. #7

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    5

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by jmcilhinney View Post
    ...In that case, changing the application icon doesn't make sense as a concept....
    .

    jmcilhinney, thank you so much for your comments.
    Here is the need I am trying to fulfill. I use the mouse with both hands and therefore need to toggle mouse setting frequently. I do this with this little ConsoleApp shown below. Every time, I toggle mouse setting, I also want to toggle the icon pinned on the taskbar as well so that I would know what is the current setting.

    Is there any other way to toggle the displayed exe icon?

    Code:
    Module Module1
        Declare Function SwapMouseButton Lib "user32" (ByVal fSwap As Boolean) As Boolean
    
        Sub Main()
            Dim test As Boolean
            test = SwapMouseButton(True)
            If (Not (test)) Then 'current setting is normal i.e Right Handed
                'SwapMouseButton (True)'we have already done it, not needed.
                Me.Icon = My.Resources.RighHandIcon
            Else
                SwapMouseButton(False)
                Me.Icon = My.Resources.LeftHandIcon
            End If
        End Sub
    
    End Module
    Last edited by learner101; Dec 9th, 2018 at 11:15 AM.

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,264

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by learner101 View Post
    That is right, by "Exe Icon" I mean Icon displayed in Windows Explorer for the application, not the form Icon.
    That icon is only set when compiled, so you would have to write a program that would compile your console program on the fly to dynamically change the icon, that means you would have to deploy the program's source code with the installation package which is something I personally wouldn't find to be worth the effort.
    Personally I would just set an icon in the console project (or leave it the default generic icon) and not worry about it, there's extremely little reason to need to change an exe icon much.

  9. #9
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,264

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by learner101 View Post
    jmcilhinney, thank you so much for your comments.
    Here is the need I am trying to fulfill. I use the mouse with both hands and therefore need to toggle mouse setting frequently. I do this with this little ConsoleApp shown below. Every time, I toggle mouse setting, I also want to toggle the icon pinned on the taskbar as well so that I would know what is the current setting.

    Is there any other way to toggle the displayed exe icon?

    Code:
    Module Module1
        Declare Function SwapMouseButton Lib "user32" (ByVal fSwap As Boolean) As Boolean
    
        Sub Main()
            Dim test As Boolean
            test = SwapMouseButton(True)
            If (Not (test)) Then 'current setting is normal i.e Right Handed
                'SwapMouseButton (True)'we have already done it, not needed needed.
                Me.Icon = My.Resources.RighHandIcon
            Else
                SwapMouseButton(False)
                Me.Icon = My.Resources.LeftHandIcon
            End If
        End Sub
    
    End Module
    Sounds like what you're really after is a console program that when ran will display an icon in the system tray (NotificationIcon) that will display either the RightHandIcon or LeftHandIcon based on which mode the system's mouse is set to?
    The NotifyIcon is a component of WindowsForms (can be used in window-less console apps) that has the ability to be changed while running.

    I can send a code example of how to do the above if that sounds like what you're trying to do.

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    5

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by JuggaloBrotha View Post
    Sounds like what you're really after is a console program that when ran will display an icon in the system tray (NotificationIcon) that will display either the RightHandIcon or LeftHandIcon based on which mode the system's mouse is set to?
    The NotifyIcon is a component of WindowsForms (can be used in window-less console apps) that has the ability to be changed while running.

    I can send a code example of how to do the above if that sounds like what you're trying to do.
    Thanks, JuggaloBrotha for your response. Kindly share the sample.

    System icon's are not visible at all times (when there are multiple icons), it requires to click on the little arrow to show all the icons, therefore, it is not exactly what the requirements is.

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,264

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by learner101 View Post
    Thanks, JuggaloBrotha for your response. Kindly share the sample.

    System icon's are not visible at all times (when there are multiple icons), it requires to click on the little arrow to show all the icons, therefore, it is not exactly what the requirements is.
    Short and simple all you really need is an ApplicationContext and NotifyIcon. I have my project set to run as a "Windows Forms Application" and it's set to use "Sub Main" as the Startup Object:
    vb.net Code:
    1. Imports Microsoft.Win32
    2.  
    3. Public Module Module1
    4.  
    5.     'Holds the application's spot in the context pool
    6.     Private m_AppContext As ApplicationContext
    7.  
    8.     'Notification icon
    9.     Private WithEvents m_Ni As NotifyIcon
    10.  
    11.     'ContextMenu for notification icon
    12.     Private m_ContextMenu As New ContextMenuStrip
    13.  
    14.     'Menu item to exit the app from the tray
    15.     Private WithEvents ExitToolStripMenuItem As New ToolStripMenuItem With {.Text = "E&xit"} ', .Image = My.Resources._Exit}
    16.  
    17.     <STAThread()> _
    18.     Public Sub Main()
    19.         'Event handler in case the system shuts down or restarts
    20.         AddHandler SystemEvents.SessionEnding, AddressOf OnShuttingdown
    21.  
    22.         m_ContextMenu.Items.AddRange(New ToolStripItem() {ExitToolStripMenuItem})
    23.         m_Ni = New NotifyIcon With {.Visible = True, .Icon = SystemIcons.Warning, .ContextMenuStrip = m_ContextMenu, .Text = "System Tray Example"}
    24.  
    25.         'Creates new context pool instance
    26.         m_AppContext = New ApplicationContext
    27.  
    28.         'Keeps the app running in the pool
    29.         Application.Run(m_AppContext)
    30.  
    31.         'Clean up
    32.         RemoveHandler SystemEvents.SessionEnding, AddressOf OnShuttingdown
    33.  
    34.         m_Ni.Visible = False
    35.  
    36.         m_ContextMenu.Dispose()
    37.         ExitToolStripMenuItem.Dispose()
    38.         m_Ni.Dispose()
    39.     End Sub
    40.  
    41.     Private Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
    42.         'Windows is shutting down (or restarting)
    43.         m_AppContext.ExitThread()
    44.     End Sub
    45.  
    46.     Private Sub ExitToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
    47.         'Close the program
    48.         m_AppContext.ExitThread()
    49.     End Sub
    50.  
    51. End Module

    As for the OS hiding the icon in the hidden tray, you can set Windows to always display your icon once you've ran the program once.

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    101,658

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Quote Originally Posted by learner101 View Post
    System icon's are not visible at all times (when there are multiple icons), it requires to click on the little arrow to show all the icons, therefore, it is not exactly what the requirements is.
    That's a Windows setting though. That's nothing to do with your application.

    Hopefully this thread demonstrates why you need to provide a FULL and CLEAR explanation at the start. If you'd told us what you were actually trying to achieve, you could have had your answer three days ago. I've got a thread in the CodeBank that covers creating an application with an icon in the system try and no form displayed:

    http://www.vbforums.com/showthread.php?636812

    I could have directed you to that and told you to change the Icon property of the NotifyIcon back in post #2.

  13. #13

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    5

    Re: Change Exe Icon Dynamically - "Me is not valid within a module"

    Thanks JuggaloBrotha and jmcilhinney.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width