Loading a Form Icon in VB.NET

In this article, I will show you how to load an icon of a Form programmatically.
  • 2453

In .NET framework, the Icon class represents a Windows icon, which is a small bitmap image used to represent an object. The icon class is defined in System.Drawing namespace. So before using Icon class, you need to reference the System.Drawing namespace to your application.

You can set icon of a form at run-time as well as at design-time. You use a form's Icon property to set form's icon at design-time. Which actually adds two lines to your source code:

Dim resources As New System.Resources.ResourceManager(GetType(Form1))
Me.IconCType(resources.GetObject("$this.Icon"), System.Drawing.Icon)

Creating an Icon Object at run-time.

You can create an icon object at run-time and set it as icon of a Form. You use Icon constructor to create an icon object. The Icon constructor is an overloaded method:

In this sample example, I load tst.ico file as an icon of the form.

Dim ico As New System.Drawing.Icon("c:\temp\tst.ico")
Me.Icon = ico 

You can even set icon's size by using other overloaded constructors.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.