|
-
Apr 2nd, 2018, 02:31 AM
#7
Thread Starter
Lively Member
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by Sitten Spynne
Actually you could pull it off but it'd take some magic. A lot of what JMC said is false and imagination reveals the truth.
You can make your own type converter for the property that converts a String to an Image. That part doesn't take much imagination. How do you represent an Image as a String? Well, you have options.
The String could represent the name of some embedded resource. Then it'd be the converter's job to get that image. Or: you could encode the image as a Base64 string, and the type converter could decode it to a Byte array, then convert that to an Image.
Thank you for your valuable answer my friend!!! I'll study the informations you gave me for sure. Last days that I am looking for answers to similarly questions, almost everywhere, I read about this Type Converter and I think is a part which I should study for sure.
However, I took a working solution from a guy from an other programming community and I would like to share it with you.
He wrote:
While the System.ComponentModel.DefaultValueAttribute does not support this, you can use the old-style ResetPropertyName and ShouldSerializePropertyName methods to achieve the same function.
Code:
Imports System.ComponentModel
Public Class MyUserControl
Private Image_ As Image = My.Resources.MyImage
Public Sub New()
InitializeComponent()
ResetImage() ' set default
End Sub
<Category("Appearance")> <DisplayName("Image")> <Description("...")>
Public Property Image As Image
Get
Return NestedControl.Image
End Get
Set(ByVal value As Image)
NestedControl.Image = value
End Set
End Property
Public Sub ResetImage()
If Image IsNot Nothing Then Image.Dispose()
Image = Image_
End Sub
Public Function ShouldSerializeImage() As Boolean
Return (Image IsNot Image_)
End Function
End Class
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|