Inherited Form

I have a VB.NET form that has the following line in its Load event

Me.BackgroundImage = Image.FromFile("background.bmp")

When I added an inherited form of this form I get the following error when open the inherited form in the designer:

//////////////////////////////////////
// An error occurred while loading the document. Fix the error, and then try loading the document again.
// The error message follows:
//
// background.bmp
/////////////////////////////////////

What is the problem?
f_asmaaAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
This gets rid of the error but then you can only see the BackgroundImage at runtime and not at designtime:

Public Class Form2
    Inherits Form1

End Class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Me.DesignMode Then
            Me.BackgroundImage = Image.FromFile("a.bmp")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f2 As New Form2
        f2.Show()
    End Sub
End Class
0
 
planoczCommented:
Have you tried using the full path of your bmp?
0
 
Éric MoreauSenior .Net ConsultantCommented:
You have to be very carefull about what you put in the Load event (and some others) of base forms because these events are executed when you try to display a derived form in the IDE designer.

The solution is to create your own method (something like MyLoad) that you will call yourself just after you did the declaration:
dim x as new MyBaseFrom()
x.MyLoad
0
 
toddhdCommented:
The problem is it is not finding the bitmap, for some reason. Can you post some code showing how you inherit and display the form?

optionally, you can comment out the offending line for the moment, then add something that creates a file, for instance, a text file. Like this:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Me.BackgroundImage = Image.FromFile("background.bmp")
        File.Create("WhereAmI.txt")
    End Sub

Now fire up the inherited form, and then go look for the WhereAmI.txt file, and you'll know where the app is looking to find the bitmap.
0
All Courses

From novice to tech pro — start learning today.