Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am just 14.. new to the world of coding... so sorry if this question is stupid.
i just created a calculator using winforms now..how do i set an icon for that.. i mean the icon of the executable and the icon at the top left of the window.
i am not using any ide...just text editor... plz help
Posted
Comments
ZurdoDev 1-Dec-15 10:51am    
Look at your Project properties. I don't recall the actual property but it might be Icon. This is actually a great way to poke around all the different options, you'll learn a lot.
Sergey Alexandrovich Kryukov 1-Dec-15 11:22am    
Agree. Instead of recalling, I usually advise the way to find it out, instead of the recipe, which does not worth remembering. Please see Solution 1.
—SA
Sergey Alexandrovich Kryukov 1-Dec-15 11:27am    
Interesting idea and important interesting question. I voted 5 for it.
Yes, this is quite possible; please see Solution 1.
—SA

Not applicable to C#: You need to add the icon file to your project, and put its reference in the resource file. See https://msdn.microsoft.com/en-us/library/aa380599.aspx[^] for more information.

You may find it easier to get hold of one of Microsoft's free copies of Visual Studio from https://www.visualstudio.com/products/visual-studio-express-vs[^].
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 1-Dec-15 14:06pm    
That's correct, but how do you know that the reason for the question is the lack of access to Visual Studio?

I seriously doubt it. How a beginner could created his calculator without the Studio? It's quite possible, but not very likely in this case. If he used SharpDevelop, MonoDevelop or other IDE, the icon could be added using these IDE. I assumed it was the request for some non-IDE development tool, which really makes sense in some complicated cases. Perhaps we both did not understand the nature of the question. Maybe it's all much simpler; hope the inquirer will clarify it.

—SA
Richard MacCutchan 2-Dec-15 4:01am    
Well that's the problem isn't it; you always make assumptions.
Sergey Alexandrovich Kryukov 2-Dec-15 4:06am    
I have to admit: I do sometimes... and make mistakes... always? well, don't exaggerate.
Not sure what it is this time. I still don't think that the inquirer wants to develop without VS...
—SA
Richard MacCutchan 2-Dec-15 4:23am    
Well I can't read OP's mind so I merely offer a suggestion. OP scan take it or leave it, his choice.
This is a very good idea. Sometimes you really need it.

Before I answer your question, I'll explain how to deal with icons and bitmaps in a usual way.

As a matter of fact, I do not recommend to add icon and bitmap resources via the Visual Studio editor even when it it quite suitable for some. Using a separate file is way more maintainable. One can even edit the file via Visual Studio separately and save it, but outside of the target project.

You can create a .resx node and use "Use existing file". The file you chose will first be added to the of of the project directories of your choice and referenced in the project file. It's "Copy" property will be set to "None". Then it will be referenced in the .resx file, but not embedded in it. Of course, it means that the file will be embedded in the resulting resource during the build.

Pay attention: the file will be copied from the original one, so exercise some precautions to avoid duplication. It's the best to have the original file ether outside of the project (you can later removed it), or at the exact location in your project file structure where you expect the copy.

Moreover, the node with C# auto-generated code will appear as a child node of your .resx node. The generated code is ready to use. Open this file in a text editor and locate some static property with the name close to the name of your original image file. Just use it.

On top of that, I recommend to avoid putting any icons in the client area of any forms/windows. It should be either bitmap (as opposed to icon) or, in case of WPF, XAML vector image based on Canvas element. If you read about standard-compliant icons, you will see how big they are. You are supposed to pack several different bitmap in different pixel sizes into a standard icon, which can easily make your icon way bigger than your whole output directory size with all your assemblies. Please see the section of my article where I explain it: Practical Sound Recorder with Sound Activation, 3.4. Some Funny Peculiarities. See also the icon, to find out how many bitmaps of what pixel sizes you are supposed to provide. If you fail to make them all, some OS elements in some modes will re-sample your icons automatically, in quite an ugly way.

So, reserve your icons exclusively to two cases 1) application icon (see your project's Properties), 2) window/form icons. It would be the best if you also reuse your application icon for the window/form icon for one or all of your windows or forms.

Now, I can answer your question.

First of, create some simple sample project and do all the steps I recommended above. Then remove all non-source files: all "bin" and "obj" directories, *.user, and so on. Look at your project files. Locate the references to your icon and other stuff you need. Learn how to insert them in text.

Also, you can use MSBuild API to write code which would safely manipulate your project files: https://msdn.microsoft.com/en-us/library/microsoft.build.tasks.msbuild.aspx[^].

I will be pretty easy to write your own utility to inject some predefined resources in your projects and perform the build with it. You can even embed this utility into the build itself, which is done via development of the custom Task class which can then be used in some project. You can create some "master project" which uses some custom Tasks to manipulate other projects/solutions involved in your build. Please see, in particular:
https://msdn.microsoft.com/en-us/library/microsoft.build.utilities.task.aspx[^],
https://msdn.microsoft.com/en-us/library/t9883dzc.aspx[^].

—SA
 
Share this answer
 
v4
Comments
Philippe Mori 1-Dec-15 12:47pm    
Probably too complicate for most situations given that it can be easily done in the IDE. If OP build its own application, it should not be hard to build it once more after updating the icons.
Sergey Alexandrovich Kryukov 1-Dec-15 12:54pm    
Do you mean custom steps used to embed the image/icon without using the Studio?

No, they are not too complicated, but such development certainly should not be done for each and every project. I did not not even try to question the inquirer's reasons to go this way; I do understand that there are big and complicated projects, putting together different components; some of such project may need some extra facilities to make them more maintainable. I assume that if the inquirer did not have such complicated situation already, he would not ask his question.

—SA
Philippe Mori 1-Dec-15 13:09pm    
He explicitly say that he is new to coding in first line!
Sergey Alexandrovich Kryukov 1-Dec-15 13:25pm    
Well, thank you for bringing it to my attention. However, I usually almost ignore this kind of information, otherwise many answers would turn into the kind "learn programming in simpler projects first, then come back with more complicated questions". Actually, it would be a reasonable advice, but a more serious answer would be more useful. After all, it's never to early to get some awareness of more complicated topics, they only need to take it properly. If it is not helpful for this particular inquirer now, it could be useful for someone else, or the same person later.

I even voted 5 for this question; and it means that this inquirer if capable of interesting thoughts and can come to advanced topics soon enough.

—SA
i have recently migrated to wpf nd im using msbuild to compile my projects....
so is there a way to set the icon using msbuild?
there must be a way to do that through the .csproj file
 
Share this answer
 
Comments
CHill60 21-Dec-15 8:33am    
If you want to respond to a post or posts you must use the "Have a Question or Comment?" link next to it/them. Otherwise the poster will not be notified that you have a follow up question.
You can create another <PropertyGroup> node in the .csproj file e.g.
<PropertyGroup>
<applicationicon>myicon.ico</applicationicon>
</PropertyGroup>
The .ico file should be in the same folder as your .csproj, .cs etc files

NOTE the edit to ensure the xml tag markers are shown in this comment
Ratul Thakur 24-Dec-15 11:07am    
^_^ thanks.... this works for me.
u should have posted this as a solution.
CHill60 24-Dec-15 11:38am    
As I actually responded to your post rather than the OP I didn't think it appropriate

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900