Changing default value of a property of a control (DataGridView).
-
I am inheriting my own datagridview (say MyDataGridView) from the standard datagridview control. What I want is that certain properties of MyDataGridView should have a different default value than what its base have. For example, AllowUserToAddRows, AllowUserToDeleteRows, AllowUserToResizeRows properties should have the default values of False; so that when I drag MyDataGridView into a form in the IDE, the default values shown in the properties grid should be False. Later on, if I want to change them to True from the grid, they will be set accordingly. Is it possible somehow? Please note that I don't want to set the default value of any custom property in MyDataGridView but the properties mentioned above that are derived from the base. Thanks. VS.Net, Framework 4.0, VB.Net, C#.Net, WinForms
-
I am inheriting my own datagridview (say MyDataGridView) from the standard datagridview control. What I want is that certain properties of MyDataGridView should have a different default value than what its base have. For example, AllowUserToAddRows, AllowUserToDeleteRows, AllowUserToResizeRows properties should have the default values of False; so that when I drag MyDataGridView into a form in the IDE, the default values shown in the properties grid should be False. Later on, if I want to change them to True from the grid, they will be set accordingly. Is it possible somehow? Please note that I don't want to set the default value of any custom property in MyDataGridView but the properties mentioned above that are derived from the base. Thanks. VS.Net, Framework 4.0, VB.Net, C#.Net, WinForms
Have a try of this
Public Class Class1
Inherits DataGridViewPrivate \_AllowUserToAddRows As Boolean = False Public Shadows Property allowUserToAddRows() As Boolean Get Return \_AllowUserToAddRows End Get Set(ByVal value As Boolean) If \_AllowUserToAddRows <> value Then \_AllowUserToAddRows = value MyBase.AllowUserToAddRows = value End If End Set End Property
End Class
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
-
I am inheriting my own datagridview (say MyDataGridView) from the standard datagridview control. What I want is that certain properties of MyDataGridView should have a different default value than what its base have. For example, AllowUserToAddRows, AllowUserToDeleteRows, AllowUserToResizeRows properties should have the default values of False; so that when I drag MyDataGridView into a form in the IDE, the default values shown in the properties grid should be False. Later on, if I want to change them to True from the grid, they will be set accordingly. Is it possible somehow? Please note that I don't want to set the default value of any custom property in MyDataGridView but the properties mentioned above that are derived from the base. Thanks. VS.Net, Framework 4.0, VB.Net, C#.Net, WinForms
You just need to set the relevant properties in the constructor of you custom control like this:-
Public Class MyDataGridView
Inherits DataGridViewPublic Sub New() InitializeComponent() AllowUserToAddRows = False AllowUserToDeleteRows = False End Sub
End Class
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
Have a try of this
Public Class Class1
Inherits DataGridViewPrivate \_AllowUserToAddRows As Boolean = False Public Shadows Property allowUserToAddRows() As Boolean Get Return \_AllowUserToAddRows End Get Set(ByVal value As Boolean) If \_AllowUserToAddRows <> value Then \_AllowUserToAddRows = value MyBase.AllowUserToAddRows = value End If End Set End Property
End Class
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
I tried the following way as well as yours on the Visible property of an inherited label but yet it stays True in the properties grid when compiled and dragged in the IDE. I need to change it manually to take effect.
Imports System.ComponentModel
Public Class MyLabel
Inherits LabelPublic Sub New() MyBase.New() 'This call is required by the Component Designer. InitializeComponent() End Sub \_ Public Shadows Property Visible As Boolean Get Return MyBase.Visible End Get Set(ByVal value As Boolean) MyBase.Visible = value End Set End Property
End Class
Another point is, I tried both ways on the AutoSize property too, with the default value of False. In that case though, in the properties grid, it was showing False but actually the property remains True, as that's what I can see while executing. In order to set the value to False, I need to set it to True and then back to False again. :sigh: