Wednesday, July 30, 2008

Setting the Form Icon to an Application Resource in VB.NET

I have an application with a main form and many dialogs. I want the main form and all of the dialogs to use the same icon in the forms' title bars. So, instead of defining the Icon property on each form, I have created an application resource and added a line of code to each Form_Load method.

First, add an Icon to your Project Resources (Project/Properties then on the Resource tab). Remember the name of the icon resource you create. Mine is AppIcon in this example.

Then for each Form_Load method in the VB.NET app (for the main form and each dialog's form), I add the following:

Me.Icon = My.Resources.AppIcon

There may be even better ways to do this, but this is the simplest I have found so far.

For more information on using resources, see:

http://visualbasic.about.com/od/usingvbnet/a/ResVBNET.htm

7 comments:

Karthik Padmanabhan said...

I usually have a base form with properties that I would want all forms to inherit across the app. Set the properties (icons, fonts etc..) on the base form and let all other forms inherit from this one.

Anonymous said...

This is exactly what i was looking for. The way you did it was perfect, saving me over 700kb of duplicate resources in my application.

Thanks!

Martin said...

Wonderful, thanks a bunch!

Anonymous said...

saved me 200+kb...thanks

Anonymous said...

Very useful technique. The size of the exe was reduced from over 3MB to slightly over 1MB since duplicated resources were eliminated. Thank you very much.

Anonymous said...

Very useful technique. The size of the exe was reduced from over 3MB to slightly over 1MB since duplicated resources were eliminated. Thank you very much.

Unknown said...

Thanks a lot! I was triying, .clone, .toIcon, imagelist("image1"), ctype, and lots more, none of them working. Why they made such complicated ways? When there's three options: embeded in exe, embeded in resource file or an icon file. The simplest icons properties you need to know are size and bit depth.