|
-
Apr 1st, 2018, 05:51 AM
#1
Thread Starter
Lively Member
[RESOLVED] How can I set an image as DefaultValue Attribute to a UserControl Property?
I am working on a UserControl with multiple controls. One of them is a PictureBox and I have create an Image Property for it so user can change the image at Design Time.
Code:
Private _Image As Image = My.Resources.windows_flag_16_darkgrey
<Category("Extra Properties")> <DisplayName("Image")> <DefaultValue(GetType(Image), "???")> <Description("")>
Public Property Image As Image
Get
Return _Image
End Get
Set(ByVal value As Image)
_Image = value
PictureBox.Image = value
End Set
End Property
My problem is with DefaultValue attribute. When user do right click on Image Property in Properties Window to Reset to it's initial image, gets Nothing. I have found this way below, which resets a color property to a specific RGB color. Ιs it possible to do something similar with an image?
Code:
<Category("Extra Properties")> <DisplayName("Color")> <DefaultValue(GetType(Color), "91,91,91")> <Description("")>
Thank you in advance!!!
-
Apr 1st, 2018, 06:14 AM
#2
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
The DefaultValue attribute, like all attributes, can only be set using literal or constant values. In this case:
Code:
<DefaultValue(GetType(Color), "91,91,91")>
you're using a String literal, so it's allowed. The reason that you are able to use a String literal for a Color property is that the Color structure has an associated TypeConverter that can convert an appropriately formatted String to a Color value. If you check out the documentation for the Color structure, you will see this:
Code:
<SerializableAttribute> _
<TypeConverterAttribute(GetType(ColorConverter))> _
<DebuggerDisplayAttribute("{NameAndARGBValue}")> _
Public Structure Color
The ColorConverter class can convert a String in an appropriate format to a Color value, which you can confirm with this code:
vb.net Code:
Dim converter As New ColorConverter If converter.CanConvertFrom(GetType(String)) Then Console.WriteLine("A Color value can be created from a String") Else Console.WriteLine("A Color value can NOT be created from a String") End If
If you check out the documentation for the Image class, you'll see this:
Code:
<SerializableAttribute> _
<TypeConverterAttribute(GetType(ImageConverter))> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Image _
Inherits MarshalByRefObject _
Implements ISerializable, ICloneable, IDisposable
If you use this code:
vb.net Code:
Dim converter As New ImageConverter If converter.CanConvertFrom(GetType(String)) Then Console.WriteLine("An Image object can be created from a String") Else Console.WriteLine("An Image object can NOT be created from a String") End If
you'll see that the ImageConverter class cannot create an Image object from a String, therefore the equivalent of what you're doing for a Color property is NOT possible for an Image property. You can't use a String literal because it cannot be converted to an Image and there is no such thing as an Image literal. As such, what you want to do is simply not possible, as I believe I told you days ago, unless someone else asked the same question.
Last edited by jmcilhinney; Apr 1st, 2018 at 06:18 AM.
-
Apr 1st, 2018, 06:42 AM
#3
Thread Starter
Lively Member
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by jmcilhinney
As such, what you want to do is simply not possible, as I believe I told you days ago, unless someone else asked the same question.
Yes it was me but not in this forum. There, you gave me just a simple answer why I can't do that. But now, you just gave me THE ANSWER why I can't do that!!! Now I really know why.
As I said in other posts, I don't want just an answer kind of, yes you can, no you can't. I want to really know why I can or why I can not!!! I want to learn!!!
Thank you for this complete answer!!!
-
Apr 1st, 2018, 06:53 AM
#4
New Member
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
Hi!
I have another approach for this question if I understood what you mean.
Picturebox cold start with preseted values in design time and fed with the start picture.
Another PictureboxStore preseted .visible = false, (or in Form_load a variable PictureboxStore as image), store the same image with: PictureboxStore.image = Picturebox.image.
When user click it, run a Pucturebox.click routine where do the actions as you need.
When reset in another routine, Picturebox.image = PctureboxStore.image more: reset PictureBox for Preseted setup i.e.: picturebox.SizeMode = 2.
-
Apr 1st, 2018, 07:13 AM
#5
Thread Starter
Lively Member
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by Lindembruck
I have another approach for this question if I understood what you mean...
Thank you for your answer my friend!!! If you have some free time, can you please give me a simple code example of what you mean?
-
Apr 1st, 2018, 10:30 AM
#6
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
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.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Apr 1st, 2018, 10:46 AM
#7
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
Hmmm... I came back to correct some of what I posted and it seems that it has already been corrected. It occurred to me some time after my previous post that the TypeConverter attribute may well be able to be applied at more than just the class level and found that that is the case upon checking. As suggested, that would mean that you could apply your own custom TypeConverter to that property. My thought was to use a base-64 String but I guess any String representation could be used, as Sitten Spynne suggested. Apologies for the partially incorrect information but, in my defence, most of what I posted earlier was news to me and I only got that specific information from the documentation after reading your question. If you want to learn with so many exclamation marks, you probably ought to be doing that reading yourself.
-
Apr 1st, 2018, 06:50 PM
#8
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by jmcilhinney
Hmmm... I came back to correct some of what I posted and it seems that it has already been corrected. It occurred to me some time after my previous post that the TypeConverter attribute may well be able to be applied at more than just the class level and found that that is the case upon checking. As suggested, that would mean that you could apply your own custom TypeConverter to that property. My thought was to use a base-64 String but I guess any String representation could be used, as Sitten Spynne suggested. Apologies for the partially incorrect information but, in my defence, most of what I posted earlier was news to me and I only got that specific information from the documentation after reading your question. If you want to learn with so many exclamation marks, you probably ought to be doing that reading yourself.
To be fair, the thought of pasting the Base64 representation of the average image into the code is hideous enough to give me pause. I think I'd try the Embedded Resource approach, myself, but I've had bad experiences with that too.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Apr 1st, 2018, 09:22 PM
#9
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by Sitten Spynne
To be fair, the thought of pasting the Base64 representation of the average image into the code is hideous enough to give me pause. I think I'd try the Embedded Resource approach, myself, but I've had bad experiences with that too.
Sounds fair.
-
Apr 2nd, 2018, 02:31 AM
#10
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
-
Apr 2nd, 2018, 02:35 AM
#11
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by Simonetos The Greek
While the System.ComponentModel.DefaultValueAttribute does not support this, you can use the old-style ResetPropertyName and ShouldSerializePropertyName methods to achieve the same function.
When you say "old-style", are you aware of whether that means that it made over from VB6?
-
Apr 2nd, 2018, 02:46 AM
#12
Thread Starter
Lively Member
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
 Originally Posted by jmcilhinney
When you say "old-style", are you aware of whether that means that it made over from VB6?
He said "old-style", not me. And I don't think that it made over VB6 because it is taken from a Microsoft document here (https://docs.microsoft.com/en-us/dot...-reset-methods), with date 03/30/2017.
PS to administrators: For some reason I can't create Links!!!
-
Apr 2nd, 2018, 09:39 AM
#13
Re: How can I set an image as DefaultValue Attribute to a UserControl Property?
I personally never considered it "old style". It's more "if you need a default that can't easily be a compile-time constant (such as an image), this gives you a way to do the same thing."
Note this is kind of wonky because Images have reference semantics. I'm pretty sure every time an image loads from My.Resources it counts as a different one. The side effect is this will always serialize the Image value, which isn't the worst side effect in the world.
Doing value semantics for an Image to make this "right" is probably not worth the effort.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
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
|