469,934 Members | 2,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Default Value of Nothing for Property of type System.Drawing.Image

Hi Everyone,

I'm writing a UserControl that exposes a property of the type
System.Drawing.Image, like this:

Public Property DefaultImage() As Image
Get
Return propDefaultImage
End Get
Set(ByVal Value As Image)
propDefaultImage = Value
SetImage()
End Set
End Property

The property behaves *mostly* as expected. What I'm having trouble with is
the behavior of the property at design-time. I would like to accomplish two
things:

1) When my control is placed on a form, in the property browser, my
DefaultImage property value is shown to be "(none)". This is correct,
except it is appearing in a bold font as if the value has been changed from
the default value. When the value is "(none)" I would like it to not be
bold.

2) When the developer defines the image by clicking the "..." browse button
and selecting a bitmap, all is well: the image is set and everything does
what was intended. However, in the property browser, if the developer
selects the property value and presses the [del] button on the keyboard, the
image is not removed. I would like this to occur.

If you look at the BackgroundImage property of a Form, you can see the
behavior I am trying to emulate. I *thought* the solution would just be to
define the DefaultValue() attribute for the property to be Nothing, like
this:

<DefaultValue(Nothing)_
Public Property DefaultImage() As Image
....

DefaultValue() is overloaded 11 times and one of the possible arguments is
Object, so I figured Nothing would fit for that one. But it does not:
"Overload resolution failed...". I then tried all sorts of things like
this:

Dim NothingImage As System.Drawing.Image = Nothing

<DefaultValue(NothingImage)_
Public Property DefaultImage() As Image
....

but I can't do that because DefaultValue() requires a constant. I can't
have a constant be an object, so... I'm stuck. There's got to be something
obvious that I'm missing. Any ideas?
Thank you for your help!!

Eric
Jan 6 '07 #1
9 4790
Using Lutz Roeder's .NET Reflector, I found that the BackgroundImage property
of a Form (it actually inherits this from the Control class) looks something
like this:

<DefaultValue(CStr(Nothing))_
Public Overridable Property BackgroundImage As Image
Get
Return propBackgroundImage
End Get
Set(ByVal value As Image)
If (Not Me.BackgroundImage Is value) Then
propBackgroundImage = value
Me.Invalidate
End If
End Set
End Property

Tony

"Eric" wrote:
Hi Everyone,

I'm writing a UserControl that exposes a property of the type
System.Drawing.Image, like this:

Public Property DefaultImage() As Image
Get
Return propDefaultImage
End Get
Set(ByVal Value As Image)
propDefaultImage = Value
SetImage()
End Set
End Property

The property behaves *mostly* as expected. What I'm having trouble with is
the behavior of the property at design-time. I would like to accomplish two
things:

1) When my control is placed on a form, in the property browser, my
DefaultImage property value is shown to be "(none)". This is correct,
except it is appearing in a bold font as if the value has been changed from
the default value. When the value is "(none)" I would like it to not be
bold.

2) When the developer defines the image by clicking the "..." browse button
and selecting a bitmap, all is well: the image is set and everything does
what was intended. However, in the property browser, if the developer
selects the property value and presses the [del] button on the keyboard, the
image is not removed. I would like this to occur.

If you look at the BackgroundImage property of a Form, you can see the
behavior I am trying to emulate. I *thought* the solution would just be to
define the DefaultValue() attribute for the property to be Nothing, like
this:

<DefaultValue(Nothing)_
Public Property DefaultImage() As Image
....

DefaultValue() is overloaded 11 times and one of the possible arguments is
Object, so I figured Nothing would fit for that one. But it does not:
"Overload resolution failed...". I then tried all sorts of things like
this:

Dim NothingImage As System.Drawing.Image = Nothing

<DefaultValue(NothingImage)_
Public Property DefaultImage() As Image
....

but I can't do that because DefaultValue() requires a constant. I can't
have a constant be an object, so... I'm stuck. There's got to be something
obvious that I'm missing. Any ideas?
Thank you for your help!!

Eric
Jan 6 '07 #2
just... wow. I never would have guessed that. But, I popped it in there
and it worked (both of my needs were solved). Thank you *very* much!
--

Eric
"tlkerns" <tl*****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
Using Lutz Roeder's .NET Reflector, I found that the BackgroundImage
property
of a Form (it actually inherits this from the Control class) looks
something
like this:

<DefaultValue(CStr(Nothing))_
Public Overridable Property BackgroundImage As Image
Get
Return propBackgroundImage
End Get
Set(ByVal value As Image)
If (Not Me.BackgroundImage Is value) Then
propBackgroundImage = value
Me.Invalidate
End If
End Set
End Property

Tony

"Eric" wrote:
>Hi Everyone,

I'm writing a UserControl that exposes a property of the type
System.Drawing.Image, like this:

Public Property DefaultImage() As Image
Get
Return propDefaultImage
End Get
Set(ByVal Value As Image)
propDefaultImage = Value
SetImage()
End Set
End Property

The property behaves *mostly* as expected. What I'm having trouble with
is
the behavior of the property at design-time. I would like to accomplish
two
things:

1) When my control is placed on a form, in the property browser, my
DefaultImage property value is shown to be "(none)". This is correct,
except it is appearing in a bold font as if the value has been changed
from
the default value. When the value is "(none)" I would like it to not be
bold.

2) When the developer defines the image by clicking the "..." browse
button
and selecting a bitmap, all is well: the image is set and everything does
what was intended. However, in the property browser, if the developer
selects the property value and presses the [del] button on the keyboard,
the
image is not removed. I would like this to occur.

If you look at the BackgroundImage property of a Form, you can see the
behavior I am trying to emulate. I *thought* the solution would just be
to
define the DefaultValue() attribute for the property to be Nothing, like
this:

<DefaultValue(Nothing)_
Public Property DefaultImage() As Image
....

DefaultValue() is overloaded 11 times and one of the possible arguments
is
Object, so I figured Nothing would fit for that one. But it does not:
"Overload resolution failed...". I then tried all sorts of things like
this:

Dim NothingImage As System.Drawing.Image = Nothing

<DefaultValue(NothingImage)_
Public Property DefaultImage() As Image
....

but I can't do that because DefaultValue() requires a constant. I can't
have a constant be an object, so... I'm stuck. There's got to be
something
obvious that I'm missing. Any ideas?
Thank you for your help!!

Eric
Jan 6 '07 #3
Eric,
I normally use:

<DefaultValue(DirectCast(Nothing, Image))_
Public Property DefaultImage() As Image
...

As the value of the default is really an Image & not a String.

Granted a String Nothing has exactly the same value as an Image Nothing, the
cast is simply needed so the compiler & pick the correct overload, in this
case the object overload.
The problem with Reflector, is that it has to *guess* at what type Nothing
is, and it simply picks the first type that comes to mind...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Eric" <er**@ejproductions.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
just... wow. I never would have guessed that. But, I popped it in there
and it worked (both of my needs were solved). Thank you *very* much!
--

Eric
"tlkerns" <tl*****@discussions.microsoft.comwrote in message
news:8F**********************************@microsof t.com...
>Using Lutz Roeder's .NET Reflector, I found that the BackgroundImage
property
of a Form (it actually inherits this from the Control class) looks
something
like this:

<DefaultValue(CStr(Nothing))_
Public Overridable Property BackgroundImage As Image
Get
Return propBackgroundImage
End Get
Set(ByVal value As Image)
If (Not Me.BackgroundImage Is value) Then
propBackgroundImage = value
Me.Invalidate
End If
End Set
End Property

Tony

"Eric" wrote:
>>Hi Everyone,

I'm writing a UserControl that exposes a property of the type
System.Drawing.Image, like this:

Public Property DefaultImage() As Image
Get
Return propDefaultImage
End Get
Set(ByVal Value As Image)
propDefaultImage = Value
SetImage()
End Set
End Property

The property behaves *mostly* as expected. What I'm having trouble with
is
the behavior of the property at design-time. I would like to accomplish
two
things:

1) When my control is placed on a form, in the property browser, my
DefaultImage property value is shown to be "(none)". This is correct,
except it is appearing in a bold font as if the value has been changed
from
the default value. When the value is "(none)" I would like it to not be
bold.

2) When the developer defines the image by clicking the "..." browse
button
and selecting a bitmap, all is well: the image is set and everything
does
what was intended. However, in the property browser, if the developer
selects the property value and presses the [del] button on the keyboard,
the
image is not removed. I would like this to occur.

If you look at the BackgroundImage property of a Form, you can see the
behavior I am trying to emulate. I *thought* the solution would just be
to
define the DefaultValue() attribute for the property to be Nothing, like
this:

<DefaultValue(Nothing)_
Public Property DefaultImage() As Image
....

DefaultValue() is overloaded 11 times and one of the possible arguments
is
Object, so I figured Nothing would fit for that one. But it does not:
"Overload resolution failed...". I then tried all sorts of things like
this:

Dim NothingImage As System.Drawing.Image = Nothing

<DefaultValue(NothingImage)_
Public Property DefaultImage() As Image
....

but I can't do that because DefaultValue() requires a constant. I can't
have a constant be an object, so... I'm stuck. There's got to be
something
obvious that I'm missing. Any ideas?
Thank you for your help!!

Eric
Jan 7 '07 #4

Hi Eric, on 6 dec you asked this question on interoperability btwn COM
& .NET when files are in same directory.

Did you get it working without resorting to using regasm /CODEBASE ?

i am asking because i am similarly stuck. thanks

Hello,

I have a .NET dll, it's in the c:\myfiles\bin directory. I went into a

Visual Studio 2005 Command Prompt changed directory to the
c:\myfiles\bin directory and ran a regasm /tlb MyDotNet.dll, so I
inside my c:\myfiles\bin directory I have MyDotNet.dll and
MyDotNet.tlb.
I then start up a VBScript test harness like so:
Dim objMyDotNet
Dim mystr
On Error Resume Next
msgbox "Getting ready to create the object."
Set objMyDotNet = CreateObject("MyDotNet.clsMyClass")
If Err.Number 0 Then
MsgBox "Error # " & Err.Number & " occurred, description: " &
Err.Description & " Hex: " & Hex(Err.Number) & " Source: " &
Err.Source
Err.Clear
End If
However, I get the message:
Error #424 occurred, description: Object required Hex: 1A7 Source:
Microsoft VBScript runtime error.
If I then run
regasm /codebase MyDotNet.dll
the error goes away.
>From what I read in Adam Nathan's ".NET and COM The Complete

Interoperability Guide:"

/codebase registers the location of the assembly file under every CLSID

with a CodeBase value. With this value registered, the CLR can locate
assemblies anywhere in the file system ... This is a fallback
mechanism, so if the registered assembly is found in the GAC or in the
local directory, the CodeBase value is not used (my .NET dll is
strongly named in case you're wondering).
Well my .tlb and .dll are in the same directory, so why do I need the
/codebase option? Does he mean that the calling COM application would
have to be in the same directory as the .dll? If this is true, could
the .tlb file be located elsewhere in the file system?
Thanks,
Eric

Jan 7 '07 #5
ah- understood. Thank you!

--
Eric


"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:Ov*************@TK2MSFTNGP04.phx.gbl...
Eric,
I normally use:

<DefaultValue(DirectCast(Nothing, Image))_
Public Property DefaultImage() As Image
...

As the value of the default is really an Image & not a String.

Granted a String Nothing has exactly the same value as an Image Nothing,
the cast is simply needed so the compiler & pick the correct overload, in
this case the object overload.
The problem with Reflector, is that it has to *guess* at what type Nothing
is, and it simply picks the first type that comes to mind...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Eric" <er**@ejproductions.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>just... wow. I never would have guessed that. But, I popped it in there
and it worked (both of my needs were solved). Thank you *very* much!
--

Eric
"tlkerns" <tl*****@discussions.microsoft.comwrote in message
news:8F**********************************@microso ft.com...
>>Using Lutz Roeder's .NET Reflector, I found that the BackgroundImage
property
of a Form (it actually inherits this from the Control class) looks
something
like this:

<DefaultValue(CStr(Nothing))_
Public Overridable Property BackgroundImage As Image
Get
Return propBackgroundImage
End Get
Set(ByVal value As Image)
If (Not Me.BackgroundImage Is value) Then
propBackgroundImage = value
Me.Invalidate
End If
End Set
End Property

Tony

"Eric" wrote:

Hi Everyone,

I'm writing a UserControl that exposes a property of the type
System.Drawing.Image, like this:

Public Property DefaultImage() As Image
Get
Return propDefaultImage
End Get
Set(ByVal Value As Image)
propDefaultImage = Value
SetImage()
End Set
End Property

The property behaves *mostly* as expected. What I'm having trouble
with is
the behavior of the property at design-time. I would like to
accomplish two
things:

1) When my control is placed on a form, in the property browser, my
DefaultImage property value is shown to be "(none)". This is correct,
except it is appearing in a bold font as if the value has been changed
from
the default value. When the value is "(none)" I would like it to not
be
bold.

2) When the developer defines the image by clicking the "..." browse
button
and selecting a bitmap, all is well: the image is set and everything
does
what was intended. However, in the property browser, if the developer
selects the property value and presses the [del] button on the
keyboard, the
image is not removed. I would like this to occur.

If you look at the BackgroundImage property of a Form, you can see the
behavior I am trying to emulate. I *thought* the solution would just
be to
define the DefaultValue() attribute for the property to be Nothing,
like
this:

<DefaultValue(Nothing)_
Public Property DefaultImage() As Image
....

DefaultValue() is overloaded 11 times and one of the possible arguments
is
Object, so I figured Nothing would fit for that one. But it does not:
"Overload resolution failed...". I then tried all sorts of things like
this:

Dim NothingImage As System.Drawing.Image = Nothing

<DefaultValue(NothingImage)_
Public Property DefaultImage() As Image
....

but I can't do that because DefaultValue() requires a constant. I
can't
have a constant be an object, so... I'm stuck. There's got to be
something
obvious that I'm missing. Any ideas?
Thank you for your help!!

Eric
Jan 8 '07 #6
Hi xamman,

I'm sorry, but that Eric wasn't me. I've used COM DLLs in .NET before, but
never when they are in the same directory. I'll have a look and see if I
can help, but I suggest you post again at the top of this thread so that
someone smarter than me can have a look. ;)
--

Eric
"xamman" <xa******@yahoo.comwrote in message
news:11**********************@s80g2000cwa.googlegr oups.com...
>
Hi Eric, on 6 dec you asked this question on interoperability btwn COM
& .NET when files are in same directory.

Did you get it working without resorting to using regasm /CODEBASE ?

i am asking because i am similarly stuck. thanks

Hello,

I have a .NET dll, it's in the c:\myfiles\bin directory. I went into a

Visual Studio 2005 Command Prompt changed directory to the
c:\myfiles\bin directory and ran a regasm /tlb MyDotNet.dll, so I
inside my c:\myfiles\bin directory I have MyDotNet.dll and
MyDotNet.tlb.
I then start up a VBScript test harness like so:
Dim objMyDotNet
Dim mystr
On Error Resume Next
msgbox "Getting ready to create the object."
Set objMyDotNet = CreateObject("MyDotNet.clsMyClass")
If Err.Number 0 Then
MsgBox "Error # " & Err.Number & " occurred, description: " &
Err.Description & " Hex: " & Hex(Err.Number) & " Source: " &
Err.Source
Err.Clear
End If
However, I get the message:
Error #424 occurred, description: Object required Hex: 1A7 Source:
Microsoft VBScript runtime error.
If I then run
regasm /codebase MyDotNet.dll
the error goes away.
>>From what I read in Adam Nathan's ".NET and COM The Complete


Interoperability Guide:"

/codebase registers the location of the assembly file under every CLSID

with a CodeBase value. With this value registered, the CLR can locate
assemblies anywhere in the file system ... This is a fallback
mechanism, so if the registered assembly is found in the GAC or in the
local directory, the CodeBase value is not used (my .NET dll is
strongly named in case you're wondering).
Well my .tlb and .dll are in the same directory, so why do I need the
/codebase option? Does he mean that the calling COM application would
have to be in the same directory as the .dll? If this is true, could
the .tlb file be located elsewhere in the file system?
Thanks,
Eric
Jan 8 '07 #7
doh- same sort of error with:

DefaultValue(DirectCast(Nothing, Image))

Error: "Conversion from 'System.Drawing.Image' to 'System.Object' cannot
occur within a constant expression."

Looks like I'll be sticking with CStr(Nothing).
Thanks again for your help, everyone!

Eric
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:Ov*************@TK2MSFTNGP04.phx.gbl...
Eric,
I normally use:

<DefaultValue(DirectCast(Nothing, Image))_
Public Property DefaultImage() As Image
...

As the value of the default is really an Image & not a String.

Granted a String Nothing has exactly the same value as an Image Nothing,
the cast is simply needed so the compiler & pick the correct overload, in
this case the object overload.
The problem with Reflector, is that it has to *guess* at what type Nothing
is, and it simply picks the first type that comes to mind...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Eric" <er**@ejproductions.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>just... wow. I never would have guessed that. But, I popped it in there
and it worked (both of my needs were solved). Thank you *very* much!
--

Eric
"tlkerns" <tl*****@discussions.microsoft.comwrote in message
news:8F**********************************@microso ft.com...
>>Using Lutz Roeder's .NET Reflector, I found that the BackgroundImage
property
of a Form (it actually inherits this from the Control class) looks
something
like this:

<DefaultValue(CStr(Nothing))_
Public Overridable Property BackgroundImage As Image
Get
Return propBackgroundImage
End Get
Set(ByVal value As Image)
If (Not Me.BackgroundImage Is value) Then
propBackgroundImage = value
Me.Invalidate
End If
End Set
End Property

Tony

"Eric" wrote:

Hi Everyone,

I'm writing a UserControl that exposes a property of the type
System.Drawing.Image, like this:

Public Property DefaultImage() As Image
Get
Return propDefaultImage
End Get
Set(ByVal Value As Image)
propDefaultImage = Value
SetImage()
End Set
End Property

The property behaves *mostly* as expected. What I'm having trouble
with is
the behavior of the property at design-time. I would like to
accomplish two
things:

1) When my control is placed on a form, in the property browser, my
DefaultImage property value is shown to be "(none)". This is correct,
except it is appearing in a bold font as if the value has been changed
from
the default value. When the value is "(none)" I would like it to not
be
bold.

2) When the developer defines the image by clicking the "..." browse
button
and selecting a bitmap, all is well: the image is set and everything
does
what was intended. However, in the property browser, if the developer
selects the property value and presses the [del] button on the
keyboard, the
image is not removed. I would like this to occur.

If you look at the BackgroundImage property of a Form, you can see the
behavior I am trying to emulate. I *thought* the solution would just
be to
define the DefaultValue() attribute for the property to be Nothing,
like
this:

<DefaultValue(Nothing)_
Public Property DefaultImage() As Image
....

DefaultValue() is overloaded 11 times and one of the possible arguments
is
Object, so I figured Nothing would fit for that one. But it does not:
"Overload resolution failed...". I then tried all sorts of things like
this:

Dim NothingImage As System.Drawing.Image = Nothing

<DefaultValue(NothingImage)_
Public Property DefaultImage() As Image
....

but I can't do that because DefaultValue() requires a constant. I
can't
have a constant be an object, so... I'm stuck. There's got to be
something
obvious that I'm missing. Any ideas?
Thank you for your help!!

Eric
Jan 8 '07 #8
VS 2003 (.NET 1.x) or VS 2005 (.NET 2.0)?

The sample I gave compiles in VS 2005; .NET 2.0 has changed what is allowed
to be passed as a parameter to an attribute.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Eric" <er**@ejproductions.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
doh- same sort of error with:

DefaultValue(DirectCast(Nothing, Image))

Error: "Conversion from 'System.Drawing.Image' to 'System.Object' cannot
occur within a constant expression."

Looks like I'll be sticking with CStr(Nothing).
Thanks again for your help, everyone!

Eric
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:Ov*************@TK2MSFTNGP04.phx.gbl...
>Eric,
I normally use:

<DefaultValue(DirectCast(Nothing, Image))_
Public Property DefaultImage() As Image
...

As the value of the default is really an Image & not a String.

Granted a String Nothing has exactly the same value as an Image Nothing,
the cast is simply needed so the compiler & pick the correct overload, in
this case the object overload.
The problem with Reflector, is that it has to *guess* at what type
Nothing is, and it simply picks the first type that comes to mind...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

<<snip>>

Jan 8 '07 #9
ah- well that would make sense. 2003 1.x
Sorry I didn't specify that up-front.
--

Eric
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:ex**************@TK2MSFTNGP04.phx.gbl...
VS 2003 (.NET 1.x) or VS 2005 (.NET 2.0)?

The sample I gave compiles in VS 2005; .NET 2.0 has changed what is
allowed to be passed as a parameter to an attribute.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Eric" <er**@ejproductions.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>doh- same sort of error with:

DefaultValue(DirectCast(Nothing, Image))

Error: "Conversion from 'System.Drawing.Image' to 'System.Object' cannot
occur within a constant expression."

Looks like I'll be sticking with CStr(Nothing).
Thanks again for your help, everyone!

Eric
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.netwrote in
message news:Ov*************@TK2MSFTNGP04.phx.gbl...
>>Eric,
I normally use:

<DefaultValue(DirectCast(Nothing, Image))_
Public Property DefaultImage() As Image
...

As the value of the default is really an Image & not a String.

Granted a String Nothing has exactly the same value as an Image Nothing,
the cast is simply needed so the compiler & pick the correct overload,
in this case the object overload.
The problem with Reflector, is that it has to *guess* at what type
Nothing is, and it simply picks the first type that comes to mind...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net

<<snip>>
Jan 9 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Toby Mathews | last post: by
1 post views Thread by Martijn Mulder | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.