GeniuS BraiN GeniuS BraiN - 3 months ago 22
Vb.net Question

Design-time changes to user control property are not reachable by run-time code

Basically I'm trying to create a user control that inherits

PictureBox
. One of the changes I want to make to the new picturebox is to add an
Opacity
property.

Everything seems to work fine except that when I change the Opacity value during design-time, that change doesn't affect the actual value of the property during run-time!

Here's my code:

Private _opacity As Integer
Public Property Opacity() As Integer
Get
Return _opacity
End Get
Set(ByVal value As Integer)
_opacity = value
If Image IsNot Nothing Then
MyBase.Image = ChangeOpacity(_image, value)
End If
End Set
End Property

Private _image As Image
Public Shadows Property Image() As Image
Get
Return _image
End Get
Set(ByVal value As Image)
Dim bmp As Bitmap = value

If bmp IsNot Nothing Then
_image = ChangeOpacity(bmp, Opacity)
Else
_image = bmp
End If
MyBase.Image = _image
End Set
End Property





Well, I just used a different approach to avoid the problem. I overrode
OnPaint
method, moved the code of
ChangeOpacity
method to it, and removed the shadowed property:
Image
since I don't need it anymore. Now the problem is gone. But I'm still curious why the design-time changes to the
Opacity
property were not saved, and why it's saved now?!

Here's my new working code. It might help someone:

Public Class Pic
Inherits PictureBox

Private _opacity As Integer
Public Property Opacity() As Integer
Get
Return _opacity
End Get
Set(ByVal value As Integer)
_opacity = value
End Set
End Property

Protected Overrides Sub OnPaint(pe As PaintEventArgs)
If Image IsNot Nothing Then
Dim Gr As Graphics = pe.Graphics
Dim colormatrix As New ColorMatrix
colormatrix.Matrix33 = Opacity / 100
Dim imgAttribute As New ImageAttributes
imgAttribute.SetColorMatrix(colormatrix, ColorMatrixFlag.[Default], ColorAdjustType.Bitmap)
Gr.DrawImage(Image, New Rectangle(0, 0, Image.Width, Image.Height), 0, 0, Image.Width, Image.Height,
GraphicsUnit.Pixel, imgAttribute)
Else
MyBase.OnPaint(pe)
End If
End Sub

End Class

Max Max
Answer

Yes it's called and used but with the default Opacity value no matter what the design time value is!.

The variables get assigned their designtime-values in a single method defined by the designer. The designer probably initializes the Image variable first.

Comments