Solved

Windows form icon

Posted on 2004-10-22
207 Views
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....

Reddy
0
Question by:angireddy
    8 Comments
     
    LVL 10

    Expert Comment

    by:james-ct16
    hi

    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

    james

    0
     

    Author Comment

    by:angireddy
    Hello Mr. James,

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

    Please let me know
    0
     
    LVL 10

    Expert Comment

    by:james-ct16
    hi

    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.

    james
    0
     
    LVL 14

    Expert Comment

    by:ptakja
    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.
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    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.
    0
     
    LVL 18

    Expert Comment

    by:armoghan
    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)
    0
     
    LVL 10

    Expert Comment

    by:james-ct16
    Hi

    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

    James

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

    Accepted Solution

    by:
    hi
    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
    icoStream.close()


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

    james
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now