Setting a default property for user control

  • Thread starter Thread starter Marty McFly
  • Start date Start date
M

Marty McFly

Hello,

I have a control class that inherits from System.Web.UI.WebControls.Button.
When I drag this control from the "My User Controls" tab in the toolbox onto
the form, I want it to reflect the following default properties: Height =
32px, Width = 144px.

I declare the Width property in my control as...

\\\
<DefaultValueAttribute(GetType(Unit), "144px"), _
DescriptionAttribute("The width of the Cancel button.")> _
_
Public Overrides Property Width() As Web.UI.WebControls.Unit
Get
Return Me.Width
End Get
Set(ByVal Value As Web.UI.WebControls.Unit)
Me.Width = Value
End Set
End Property
///

After building the project, the Width in the Properties pane of the IDE is
disabled and displays the text: "Exception of type
System.StackOverflowException was thrown.".

I also tried to use the following line of code, but it barked about
requiring a constant expression:
<DefaultValueAttribute(Unit.Pixel(144)), _ ...

Does anyone have any ideas? Many thanks!!

Take care,

Marty
 
Wow, nobody answering questions today? :-)

I finally came up with a VERY simple way to make the default values show up
in the designer. (I can't believe that I couldn't find something so basic
on MSDN or Google!) Hope this helps someone having the same problem!

Marty

\\\
' Used with property attributes:
Imports System.ComponentModel
' Used with the Unit and Button types:
Imports System.Web.UI.WebControls
..
..
..
Public Class myButton
Inherits Button

' The default width should be 144 pixels:
Private _Width As Unit = Unit.Pixel(144)

<CategoryAttribute("Application"), _
Browsable(True), _
DescriptionAttribute("The width (in pixels) of the button.")> _
_
Public Overrides Property Width() As Unit
Get
Return _Width
End Get
Set(ByVal Value As Unit)
_Width = Value
Me.Width = _Width
End Set
End Property
End Class
///
 
You should use attributes like those bellow, applied to the class .

DefaultEvent("YouEventPropName"),DefaultProperty("YourPropName")



<DefaultProperty("Width")> _
Public Class MyButton

'....

End Class



Ernest
 
Back
Top