Windows form icon

I have an icon for the windows form, when I added the icon the generated code was

me.Icon = CType(resources.GetObject($this.Icon"), System.Drawing.Icon)

when I start the application, its throwing an error saying "Specified cast is not valid"

Please let me know how to solve the problem....

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

james-ct16Connect With a Mentor Commented:
just rechecking the post if you havent noticed the error already i made two errors while typing out this code as cut some bits out

line 6
icoStream = eA.GetManifestResourceStream(thisNamespace & ".icon.ico")
should of course read
icoStream = eA.GetManifestResourceStream(thisNamespace & iconName)

line 9
Insert line

Sorry about those, i should have just copy and pasted. instead of just double checking it all worked in vs first


this might solve your problem but i tend not to use the resource librarys that much as some of us still have to support 98 and earlier os which dont support without modifcation.

Me.Icon = LoadResPicture($this.Icon, vbResIcon)  

good luck, let me know if it doesnt work and ill break out visual studio and have a play


angireddyAuthor Commented:
Hello Mr. James,

What is LoadResPicture function, is that in built of do we have to call that function.

Please let me know
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.


its one i have used a couple of times at home, i thought it was inbuilt but in i have here its not

i shall be home in a couple of hours and dig the project up for you.

I have run into this same problem. But I can't exactly remember how to fix it.

Try going deleting all the files in your project's /bin and /obj directories and then rebuild the project.
try deleting the resource file and creating it again as probably the path name $this.Icon is corrupted in it or the icon itself thats way it cannot load it.
you may be able to do something like this as well, if you do not want the generated code and want to load it from path
Dim img As Image = Image.FromFile(path)

sorry this took me so long to get back to you with, somewhat hecktic moving house, ignoring that this is the little wrapper. Be aware the names from a resource file are case sensitive. Also you probably couldhardcode in the the namespace. also you probably could get rid of the dim ico and just set the me.icon to the new icon from the stream, would have made it a bit less readable though.
In addition you may which to add some try catch statements to detect when the code is not fully trusted as it may well fall over.
This code will do the job you want though.

Hope this solves your problem


code follows

Private Function LoadResPicture(Optional ByVal iconName As String = "icon.ico")
        Dim eA As System.Reflection.Assembly = Me.GetType.Assembly.GetEntryAssembly
        Dim thisNamespace As String = eA.GetName().Name.ToString

        Dim ico As Icon
        Dim icoStream As System.IO.Stream

        icoStream = eA.GetManifestResourceStream(thisNamespace & ".icon.ico")
        If Not icoStream Is Nothing Then
            ico = New Icon(icoStream)
            Me.Icon = ico
        End If
    End Function
All Courses

From novice to tech pro — start learning today.