Results 1 to 13 of 13

Thread: [RESOLVED] How can I set an image as DefaultValue Attribute to a UserControl Property?

  1. #1

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Resolved [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!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim converter As New ColorConverter
    2.  
    3. If converter.CanConvertFrom(GetType(String)) Then
    4.     Console.WriteLine("A Color value can be created from a String")
    5. Else
    6.     Console.WriteLine("A Color value can NOT be created from a String")
    7. 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:
    1. Dim converter As New ImageConverter
    2.  
    3. If converter.CanConvertFrom(GetType(String)) Then
    4.     Console.WriteLine("An Image object can be created from a String")
    5. Else
    6.     Console.WriteLine("An Image object can NOT be created from a String")
    7. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by jmcilhinney View Post
    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!!!

  4. #4
    New Member Lindembruck's Avatar
    Join Date
    Mar 2018
    Posts
    4

    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.

  5. #5

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by Lindembruck View Post
    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?

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by jmcilhinney View Post
    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by Sitten Spynne View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by Sitten Spynne View Post
    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

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by Simonetos The Greek View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Lively Member Simonetos The Greek's Avatar
    Join Date
    Mar 2018
    Location
    Athens, Greece
    Posts
    65

    Re: How can I set an image as DefaultValue Attribute to a UserControl Property?

    Quote Originally Posted by jmcilhinney View Post
    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!!!

  13. #13
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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
  •  



Click Here to Expand Forum to Full Width