Recently I was working on a small Visual Basic Windows form application and decided to add an icon to make it look nice on my desktop. After adding the icon to the application I added the same icon to each of the forms in the application to dress them up a bit. It seems every time I added the icon to a form Visual Basic was adding another copy of the icon to the executable.

It took a while but I finally found a way to reuse the applications icon on each form to save space. In the Load event of each form I extracted the icon from the application and assigned it to the form.

The base Load event handler looks like this.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath)
End Sub

After getting the icon from the executable file for the main application window, the Load event handler for the rest of the windows can refer to the main form to avoid more file access.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Icon = My.Application.OpenForms(0).Icon
End Sub