[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Windows form icon

Posted on 2004-10-22
Medium Priority
Last Modified: 2012-05-05
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....

Question by:angireddy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Expert Comment

ID: 12381853

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



Author Comment

ID: 12382194
Hello Mr. James,

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

Please let me know
LVL 10

Expert Comment

ID: 12382251

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

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 14

Expert Comment

ID: 12386174
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.
LVL 18

Expert Comment

ID: 12397400
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.
LVL 18

Expert Comment

ID: 12397415
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)
LVL 10

Expert Comment

ID: 12398202

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

Accepted Solution

james-ct16 earned 1500 total points
ID: 12398295
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


Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question