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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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

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.

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.