Setting default property value in user control
Hi all,
I am creating a customised version of the ListView control and there are several of the properties that a ListView has that I would like to be set to a different value to the ListView default when a user goes to use my user control.
From what I've found there seems to be a number of people suggesting just set them in the constructor, but to the best of my knowledge that would just mean that the user would not be able to change the properties in the PropertyGrid in the VS IDE.
I assume this is probably something extremely simple that I have overlooked.
Thanks for any help in advance,
Satal :D
Re: Setting default property value in user control
Why could they not change them? The only time the defaults would be set is when you create the object, if you look at your designer files (they are hidden by default) you can see something like this:
Code:
'
'lsvConnections
'
Me.lsvConnections.Columns.AddRange(New Syst...
Me.lsvConnections.ContextMenu = Me.ctmConnections
Me.lsvConnections.Dock = System.Windows.Forms.DockStyle.Fill
Me.lsvConnections.FullRowSelect = True
Me.lsvConnections.GridLines = True
...
When you use the IDE it writes this for you, it creates an instance of the object (thus firing your default property code) then it will update any properties that the user has changed in the propertygrid.
However, possibly you can override properties (if they allow it, if not you can shadow them!) and change their structure to use a different default attribute (System.ComponentModel.DefaultValue). Heres an example of one, I have not tried it while overriding another class but I can't see that it should block you.
Code:
<System.ComponentModel.DefaultValue(True)> _
Public Property UseScript() As Boolean
Get
Return mobjData.UseScript
End Get
Set(ByVal value As Boolean)
mobjData.UseScript = value
End Set
End Property
Re: Setting default property value in user control
Well, playing with the second idea it does not work as I expected. If I add a custom control to my form, it does not use my new default value, as it calls MyBase.xxx then it uses thats default value. However, if I reset the property in the property grid it does goto my default.
If however I do a combination of the 2 ideas I get this, and it works:
Code:
Public Class TheClass : Inherits ListView
Sub New()
MyBase.Dock = DockStyle.Right
End Sub
<System.ComponentModel.DefaultValue(System.Windows.Forms.DockStyle.Right)> _
Public Overrides Property Dock() As System.Windows.Forms.DockStyle
Get
Return MyBase.Dock
End Get
Set(ByVal value As System.Windows.Forms.DockStyle)
MyBase.Dock = value
End Set
End Property
End Class
Re: Setting default property value in user control
Brilliant thanks :D that was exactly what I was looking for.
Re: Setting default property value in user control
There's a difference between default values and initial values. The initial value is what you set in the constructor and is the value that your property will have when you drag a new instance to your form. The default value, set using the DefaultValue attribute, is something completely different and is only related to the VS IDE property grid (not even the PropertyGrid control as far as I'm aware!). The property grid shows your property in bold font if the value is not equal to the default value, and you can rightclick any property and select 'Reset' to set it back to its default value. If you didn't specify any default value then the Reset option will be disabled.
It looks like what you want are initial values (although it would also be a good idea to set the default value if you can override the property). You just set them in the constructor of the control.
Re: Setting default property value in user control
Quote:
Originally Posted by
NickThissen
There's a difference between default values and initial values. The initial value is what you set in the constructor and is the value that your property will have when you drag a new instance to your form. The default value, set using the DefaultValue attribute, is something completely different and is only related to the VS IDE property grid (not even the PropertyGrid control as far as I'm aware!). The property grid shows your property in bold font if the value is not equal to the default value, and you can rightclick any property and select 'Reset' to set it back to its default value. If you didn't specify any default value then the Reset option will be disabled.
It looks like what you want are initial values (although it would also be a good idea to set the default value if you can override the property). You just set them in the constructor of the control.
wouldnt they still both be the same when u add the control to a form?
Re: Setting default property value in user control
Quote:
Originally Posted by
MattBodin
wouldnt they still both be the same when u add the control to a form?
Not necessarily. If you have a property (one that doesn't override a property in the base control) with a default value, and you never set its initial value in the constructor, then the property will NOT get the default value as its initial value. It's initial value will be whatever the default value for the type is (0 for numeric types, etc).
If you are overriding a property, then it depends on what the base control does with that property. It might set an initial value in its constructor, and it would be logical that that value was the same as the default value, but again that is not necessarily the case. A good example is the BackColor property, of a Label for example, which is set to the same color as the parent when you drop it on the form or some other control, which may not be the default control backcolor.
Try this:
Code:
Imports System.ComponentModel
Public Class CustomControl
Inherits Control
Private _Test As String
<DefaultValue("Xyz")> _
Public Property Test() As String
Get
Return _Test
End Get
Set(ByVal value As String)
_Test = value
End Set
End Property
End Class
When you drop the control on your form, the Test property is empty, and does not contain "Xyz". It will be displayed in bold (if it had a value) because it is non-default though. You can rightclick it and select Reset, and it will then get the value Xyz.
Re: Setting default property value in user control
though had to read the first couple sentences like 10 times, i think i understand; thanks for explaining.
Re: Setting default property value in user control
Quote:
Originally Posted by
MattBodin
wouldnt they still both be the same when u add the control to a form?
I thought that until I tested it. By overriding an existing property to change its default, even though I had not changed its value when I dragged it onto my form, it was not using my default. I had chosen to Override the Dock location, and due to it calling Return MyBase.Dock, I picked up the base class default not my own. So I had to do both on constructor and on declaration to make it consistent.