Reputation: 690
I am developing a custom library with custom controls. I need Visual Studio shows a Message in Design Mode when user inserts an invalid value in a custom property, as visual studio does when you try Color.Transparent for BorderColor in a Button.
A piece of code:
Public Class ButtonWithBrigthness 'It inherits from Button in .Designer.vb
Private _brigthness As Integer
Public Property Brightness() As Integer
Get
Return _brigthness
End Get
Set (value as Integer)
If (value >= 0 And value <= 10) Then
_brigthness = value
Else
'Throw Exception for an invalid value....
End If
End Set
End Property
End Class
Upvotes: 0
Views: 409
Reputation: 4806
To know if you are in Design Mode or not, you can use the DesignMode Property.
Next you need to know that if you raise an exception from a property setter in design-mode, the property grid will catch it and display a message box saying "Invalid Property Value", the user can click on "Details" to see the custom message you put in. If you want to do better you show a Message Box indicating why it's not working.
Anyway, if you want to raise an exception :
Public Property Brightness() As Integer
Get
Return _brigthness
End Get
Set (value as Integer)
'I rewrote the condition, but you don't have to, just put the exception in the else...
If value < 0 Or value > 10 Then
'Here we throw the Exception only in design mode
'So at runtime just nothing will happen...
If Me.DesignMode Then
Throw New ArgumentOutOfRangeException("Brightness must be between 0 and 10")
End If
Else
_brigthness = value
End If
End Set
End Property
And if you want to show a nice Message Box...
Public Property Brightness() As Integer
Get
Return _brigthness
End Get
Set (value as Integer)
If value < 0 Or value > 10 Then
'Here we show the MessageBox only in design mode
'So at runtime just nothing will happen...
If Me.DesignMode Then
MessageBox.Show("Brightness must be between 0 and 10", "Invalid Brightness Value")
End If
Else
_brigthness = value
End If
End Set
End Property
Upvotes: 1