dcsimg
Results 1 to 9 of 9

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

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    3

    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
    100,586

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Member
    Join Date
    Apr 2009
    Posts
    34

    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
    100,586

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    3

    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
    3

    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; Today at 11:15 AM.

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

    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,256

    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.

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