Re: Setting a default property for user control by Marty
Marty
Fri Jul 02 08:52:14 CDT 2004
Thank you for the reply, Ernest.
Actually, I was trying to give each property a default value that shows up
in the designer when adding the control, not assign a default event/property
to the parent class. The code I eventually came up with (that works) is
listed below.
Thanks again,
Marty
'\\\
' In the "Defaults" module...
Friend Const BUTTON_HEIGHT As Int16 = 32
Friend Const BUTTON_WIDTH As Int16 = 144
Friend BUTTON_BACKCOLOR As Color = _
Color.FromArgb(212, 208, 200) ' Same as #D4D0C8.
Friend BUTTON_BACKCOLOR_ONFOCUS As Color = _
Color.FromArgb(102, 255, 255) ' Same as #66FFFF.
Friend BUTTON_BACKCOLOR_ONBLUR As _
Color = Color.FromArgb(212, 208, 200)
'///
'\\\
' The "CancelButton" class.
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Drawing
<ToolboxBitmap(GetType(Button))> _
_
Public Class CancelButton
#Region " Initialization "
Inherits System.Web.UI.WebControls.Button
Private Sub InitializeComponent()
' Initially set the overridden/shadowed properties,
' so they will appear as "default" values in the
' designer.
MyBase.Font.Size = FontUnit.XSmall
MyBase.Font.Name = "Verdana"
MyBase.Width = Unit.Pixel(BUTTON_WIDTH)
MyBase.Height = Unit.Pixel(BUTTON_HEIGHT)
End Sub
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
#End Region
#Region " Properties "
Private _Text As String = "Cancel"
Private _Width As Unit = Unit.Pixel(BUTTON_WIDTH)
Private _Height As Unit = Unit.Pixel(BUTTON_HEIGHT)
Private _BackColor As Color = BUTTON_BACKCOLOR
Private _BackColorOnFocus As Color = _
BUTTON_BACKCOLOR_ONFOCUS
Private _BackColorOnBlur As Color = _
BUTTON_BACKCOLOR_ONBLUR
Public Shadows Property Text() As String
Get
Return _Text
End Get
Set(ByVal Value As String)
_Text = Value
MyBase.Text = Value
End Set
End Property
Public Overrides Property Width() As Unit
Get
Return _Width
End Get
Set(ByVal Value As Unit)
_Width = Value
MyBase.Width = Value
End Set
End Property
Public Overrides Property Height() As Unit
Get
Return _Height
End Get
Set(ByVal Value As Unit)
_Height = Value
MyBase.Height = Value
End Set
End Property
Public Overrides Property BackColor() As Color
Get
Return _BackColor
End Get
Set(ByVal Value As Color)
_BackColor = Value
MyBase.BackColor = _BackColor
End Set
End Property
<CategoryAttribute("Application"), _
Browsable(True), _
DescriptionAttribute("The background color " & _
"when this button has focus.")> _
_
Public Property BackColorOnFocus() As Color
Get
Return _BackColorOnFocus
End Get
Set(ByVal Value As Color)
_BackColorOnFocus = Value
End Set
End Property
<CategoryAttribute("Application"), _
Browsable(True), _
DescriptionAttribute("The background color " & _
"when this button loses focus.")> _
_
Public Property BackColorOnBlur() As Color
Get
Return _BackColorOnBlur
End Get
Set(ByVal Value As Color)
_BackColorOnBlur = Value
End Set
End Property
Public Overrides ReadOnly Property Font() As FontInfo
Get
Return MyBase.Font
End Get
End Property
#End Region
Private Sub CancelButton_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Set the overridden/shadowed properties.
MyBase.Text = _Text
MyBase.Width = _Width
MyBase.Height = _Height
MyBase.BackColor = _BackColor
' Add cursor enhancements, converting WebControls.Color
' type to an HTML RGB color string (ex: #FFFFFF).
Attributes.Add("onfocus", "this.style.background='" & _
ColorTranslator.ToHtml(_BackColorOnFocus) & "';")
Attributes.Add("onblur", "this.style.background='" & _
ColorTranslator.ToHtml(_BackColorOnBlur) & "';")
' Add attribute to cancel the form.
Attributes.Add("onclick", _
"CancelEdit = true; myForm.submit;")
End Sub
End Class
'///
"Ernest Morariu" <ernest@gesora.com> wrote in message
news:edsHwn6XEHA.2500@TK2MSFTNGP09.phx.gbl...
> You should use attributes like those bellow, applied to the class .
>
> DefaultEvent("YouEventPropName"),DefaultProperty("YourPropName")
>
>
>
> <DefaultProperty("Width")> _
> Public Class MyButton
>
> '....
>
> End Class
>
>
>
> Ernest
>
>
>
>
>
> "Marty McFly" <Marty@Mc.Fly> wrote in message
> news:#Q9RfHVXEHA.2940@TK2MSFTNGP09.phx.gbl...
> > 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
> > ///
> >
> >
> > "Marty McFly" <Marty@Mc.Fly> wrote in message
> > news:eX0y6jSXEHA.4020@TK2MSFTNGP09.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>