Having trouble with init of usercontrol

L

Lloyd Sheen

I am creating a simple ImageButton. I have added two properties to the
Button class EnableImage and DisabledImage to create the new usercontrol
(inherited from Button).

I can use the designer to add images to a button on a test form. This works
well.

The problem is when I run the test. There is a "regular" button on the form
along with my ImageButton. Clicking the "regular" button simply changes the
enabled status of the ImageButton. There is code in the ImageButton to
detect the enabled status of the button and set the BackgroundImage to the
appropriate image as define in the designer. This works.

What does not work is that when the form is loaded there is no image for the
button. If I look at the code in the designer (for the form) I can see the
images being loaded from the resource file.

Me.ImageButton1.DisabledImage =
Global.JustForTesting.My.Resources.Resources.Step_Forward_Disabled_48x48
Me.ImageButton1.EnabledImage =
Global.JustForTesting.My.Resources.Resources.Step_Forward_Normal_Blue_48x48

The code for toggling the image is :

Private Sub ImageButton_EnabledChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.EnabledChanged
If Me.Enabled Then
Me.BackgroundImage = EnabledImage
Else
Me.BackgroundImage = DisabledImage
End If
End Sub

There is also code in the New of the ImageButton:

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Me.BackColor = Color.Transparent
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
Me.FlatAppearance.BorderSize = 0
Me.FlatAppearance.MouseDownBackColor = Color.Transparent
Me.FlatAppearance.MouseOverBackColor = Color.Transparent
End Sub

Any ideas?

Thanks
LS
 
J

Jack Jackson

I am creating a simple ImageButton. I have added two properties to the
Button class EnableImage and DisabledImage to create the new usercontrol
(inherited from Button).

I can use the designer to add images to a button on a test form. This works
well.

The problem is when I run the test. There is a "regular" button on the form
along with my ImageButton. Clicking the "regular" button simply changes the
enabled status of the ImageButton. There is code in the ImageButton to
detect the enabled status of the button and set the BackgroundImage to the
appropriate image as define in the designer. This works.

What does not work is that when the form is loaded there is no image for the
button. If I look at the code in the designer (for the form) I can see the
images being loaded from the resource file.

Me.ImageButton1.DisabledImage =
Global.JustForTesting.My.Resources.Resources.Step_Forward_Disabled_48x48
Me.ImageButton1.EnabledImage =
Global.JustForTesting.My.Resources.Resources.Step_Forward_Normal_Blue_48x48

The code for toggling the image is :

Private Sub ImageButton_EnabledChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.EnabledChanged
If Me.Enabled Then
Me.BackgroundImage = EnabledImage
Else
Me.BackgroundImage = DisabledImage
End If
End Sub

There is also code in the New of the ImageButton:

Public Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.
Me.BackColor = Color.Transparent
Me.BackgroundImageLayout = ImageLayout.Stretch
Me.FlatStyle = Windows.Forms.FlatStyle.Flat
Me.FlatAppearance.BorderSize = 0
Me.FlatAppearance.MouseDownBackColor = Color.Transparent
Me.FlatAppearance.MouseOverBackColor = Color.Transparent
End Sub

Any ideas?

Thanks
LS

I don't see any place where you set the BackgroundImage property
except in the EnabledChanged handler. Since that handler doesn't get
called during instantiation of the object, the property never gets
set.

I would do it this way:

Private Sub SetBackgroundImage()
If Me.Enabled Then
Me.BackgroundImage = EnabledImage
Else
Me.BackgroundImage = DisabledImage
End If
' Or if using VS2008:
' Me.BackgroundImage = If(Me.Enabled, EnabledImage, DisabledImage)
End Sub

Protected Overrides Sub OnEnabledChanged(ByBal e As System.EventArgs)
SetBackgroundImage()
MyBase.OnEnabledChanged(e)
End Sub

And add at the end of New:
SetBackgroundImage()
 
L

Lloyd Sheen

Jack Jackson said:
I don't see any place where you set the BackgroundImage property
except in the EnabledChanged handler. Since that handler doesn't get
called during instantiation of the object, the property never gets
set.

I would do it this way:

Private Sub SetBackgroundImage()
If Me.Enabled Then
Me.BackgroundImage = EnabledImage
Else
Me.BackgroundImage = DisabledImage
End If
' Or if using VS2008:
' Me.BackgroundImage = If(Me.Enabled, EnabledImage, DisabledImage)
End Sub

Protected Overrides Sub OnEnabledChanged(ByBal e As System.EventArgs)
SetBackgroundImage()
MyBase.OnEnabledChanged(e)
End Sub

And add at the end of New:
SetBackgroundImage()

Thanks, that worked great.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top