Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to set Default Value to an image Public Property of a UserControl.

What I have tried:

I tried to do that with a variable but with no luck...
VB
Private Image_ As Image = My.Resources.MyImage
<Category("Appearance")> <DisplayName("Image")> <DefaultValue(Image_)>
Public Property Image As Image
    Get
        Return NestedControl.Image
    End Get
    Set(ByVal value As Image)
        NestedControl.Image = value
    End Set
End Property
I also tried to set Default Value like this..
VB
<DefaultValue(GetType(Image), "My.Resources.MyImage")>
But when I click Reset to UserControl's property it turns to "None"!!! There is any other way I can do that?
Posted
Updated 29-Mar-18 2:00am

1 solution

Try this:
VB
Private Image_ As Image = My.Resources.MyImage
<Category("Appearance")> <DisplayName("Image")> 
Public Property Image As Image
    Get
        Return Image_
    End Get
    Set(ByVal value As Image)
        Image_ = value
    End Set
End Property
 
Share this answer
 
v2
Comments
Simos Sigma 29-Mar-18 8:16am    
Nothing... DafaultValue is the problem. Gives me error "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class"
OriginalGriff 29-Mar-18 8:43am    
As an error message, that makes a lot of sense - you can't do that!
Remove the whole DefaultValue bit - I've deleted it from the solution.
Simos Sigma 29-Mar-18 8:48am    
OK! Is there any other way so I can set default value in this case?
Maciej Los 29-Mar-18 9:08am    
Take a look at your code and OriginalGriff's. Do you see the difference?
    Get
        'Return NestedControl.Image 'your
        Return Image_ 'OriginalGriff's
    End Get

In other words, you have to return Image_ when NestedControl.Image is null. ;)
Simos Sigma 29-Mar-18 9:11am    
Yes my friend of course, but this doesn't enable the "Reset" option in properties window when I do right click on Image property!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900