How to hide expand button in a propertygrid in Windows Forms - vb.net

I have written my on editor for a property in a propertyGrid. Everything works as expected, but for best user experience i want to hide the expand button on the propertygrid for this property. Can someone tell me how to do that?
I am using a custom typeconverter
Public Class PropertyLanguageSetupEditor
Inherits UITypeEditor
Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
Try
Dim svc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
Dim settings As LanguageSettings = value
Dim oldSettings As LanguageSettings = CType(value, LanguageSettings).Clone
If Not svc Is Nothing And Not settings Is Nothing Then
Dim form As New PropertyLanguageSetupWindow(settings)
If svc.ShowDialog(form) = DialogResult.OK Then
value = form.Data
Else
value = oldSettings
End If
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return MyBase.EditValue(context, provider, value)
End Function
End Class
And this is de property which uses the typeConverter
<DisplayName("Properties Translation Settings"), Editor(GetType(PropertyLanguageSetupEditor), GetType(System.Drawing.Design.UITypeEditor)), TypeConverter(GetType(BrowsableTypeConverter)),
Description("Settings for the output text"), BrowsableTypeConverter.BrowsableLabelStyleAttribute(BrowsableTypeConverter.LabelStyle.lsEllipsis),
Category("10 - DXF Export"), Browsable(True)>
Public Property TranslationSettings As LanguageSettings = New LanguageSettings
My BrowsAbleTypeConverter
Public Class BrowsableTypeConverter
Inherits ExpandableObjectConverter
Public Enum LabelStyle
lsNormal
lsTypeName
lsEllipsis
lsText
End Enum
Public Class BrowsableLabelStyleAttribute
Inherits System.Attribute
Private eLabelStyle As LabelStyle = LabelStyle.lsEllipsis
Public Sub New(ByVal LabelStyle As LabelStyle)
eLabelStyle = LabelStyle
End Sub
Public Property LabelStyle() As LabelStyle
Get
Return eLabelStyle
End Get
Set(ByVal value As LabelStyle)
eLabelStyle = value
End Set
End Property
End Class
Public Class BrowsableLabelTextAttribute
Inherits System.Attribute
Private strText As String = ""
Public Sub New(ByVal value As String)
strText = value
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal value As String)
strText = value
End Set
End Property
End Class
Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
Return True
End Function
Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
Dim Style As BrowsableLabelStyleAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelStyleAttribute))
If Not Style Is Nothing Then
Select Case Style.LabelStyle
Case LabelStyle.lsNormal
Return MyBase.ConvertTo(context, culture, value, destinationType)
Case LabelStyle.lsTypeName
Return "(" & value.GetType.Name & ")"
Case LabelStyle.lsEllipsis
Return "(...)"
Case LabelStyle.lsText
Dim text As BrowsableLabelTextAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelTextAttribute))
If text IsNot Nothing Then
Return text.Text
End If
End Select
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class

Your TypeConverter derives from ExpandableObjectConverter, which means subproperties will be visible because of its GetPropertiesSupported method returning true.
In your TypeConverter, you simply need to override GetPropertiesSupported and return false.

Related

How to assign typical NumericUpDown properties to custom NumericUpDown based control?

I'm trying to make a DataGridViewColumn that inherits all of the properties of a typical NumericUpDown control. I found an answer here on StackOverflow (https://stackoverflow.com/a/55788490/692250) and a MSDN article here (https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-host-controls-in-windows-forms-datagridview-cells?view=netframeworkdesktop-4.8&redirectedfrom=MSDN#code-snippet-1).
The code from the article works well, but... I cannot assign a Maximum nor Minimum value to the control (both are stuck at the "default" Minimum of 0 and Maximum of 100).
I tried to add in code from the SO answer (by adding in a Min and Max field) but doing so locks both in to 0 and unable to change.
The question in short: how do I add the necessary properties to allow to this work.
The custom control is question:
Public Class NumericUpDownColumn
Inherits DataGridViewColumn
Public Sub New()
MyBase.New(New NumericUpDownCell())
End Sub
Public Overrides Property CellTemplate() As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
' Ensure that the cell used for the template is a CalendarCell.
If Not (value Is Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(NumericUpDownCell)) Then
Throw New InvalidCastException("Must be an Integer")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class NumericUpDownCell
Inherits DataGridViewTextBoxCell
Public Sub New()
' Number Format
Me.Style.Format = "0"
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As NumericUpDownEditingControl = CType(DataGridView.EditingControl, NumericUpDownEditingControl)
ctl.Value = CType(Me.Value, Decimal)
End Sub
Public Overrides ReadOnly Property EditType() As Type
Get
' Return the type of the editing contol that CalendarCell uses.
Return GetType(NumericUpDownEditingControl)
End Get
End Property
Public Overrides ReadOnly Property ValueType() As Type
Get
' Return the type of the value that CalendarCell contains.
Return GetType(Decimal)
End Get
End Property
Public Overrides ReadOnly Property DefaultNewRowValue() As Object
Get
' Use the current date and time as the default value.
Return 0
End Get
End Property
End Class
Class NumericUpDownEditingControl
Inherits NumericUpDown
Implements IDataGridViewEditingControl
Private dataGridViewControl As DataGridView
Private valueIsChanged As Boolean = False
Private rowIndexNum As Integer
Public Sub New()
Me.DecimalPlaces = 0
End Sub
Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
Get
Return Me.Value.ToString("#")
End Get
Set(ByVal value As Object)
If TypeOf value Is Decimal Then
Me.Value = Decimal.Parse(value.ToString)
End If
End Set
End Property
Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
Return Me.Value.ToString("#")
End Function
Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
Me.Font = dataGridViewCellStyle.Font
Me.ForeColor = dataGridViewCellStyle.ForeColor
Me.BackColor = dataGridViewCellStyle.BackColor
End Sub
Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
Get
Return rowIndexNum
End Get
Set(ByVal value As Integer)
rowIndexNum = value
End Set
End Property
Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
' Let the DateTimePicker handle the keys listed.
Select Case key And Keys.KeyCode
Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp
Return True
Case Else
Return False
End Select
End Function
Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
' No preparation needs to be done.
End Sub
Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
Get
Return False
End Get
End Property
Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
Get
Return dataGridViewControl
End Get
Set(ByVal value As DataGridView)
dataGridViewControl = value
End Set
End Property
Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
Get
Return valueIsChanged
End Get
Set(ByVal value As Boolean)
valueIsChanged = value
End Set
End Property
Public ReadOnly Property EditingControlCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
Get
Return MyBase.Cursor
End Get
End Property
Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)
' Notify the DataGridView that the contents of the cell have changed.
valueIsChanged = True
Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
MyBase.OnValueChanged(eventargs)
End Sub
End Class
Focusing on it a bit more, I found that I can assign values to the Maximum and Minimum fields as so:
' Set the value of the editing control to the current cell value.
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
Dim ctl As NumericUpDownEditingControl = CType(DataGridView.EditingControl, NumericUpDownEditingControl)
ctl.Value = CType(Me.Value, Decimal)
ctl.Maximum = 180
ctl.Minimum = -1
End Sub
But I cannot seem to pass to this field via the usual means of .Maximum or .Minimum on the constructor. Any tips/advice?
This is an easy fix and very easy to overlook.
In your NumericUpDownCell class you need to override the Clone method to include the new Minimum and Maximum properties, for example:
First, add the new properties the NumericUpDownCell class and then override the Clone function
Public Class NumericUpDownCell
Inherits DataGridViewTextBoxCell
...
Public Property Minimum() As Integer
Public Property Maximum() As Integer
...
Public Overrides Function Clone() As Object
Dim retVal As NumericUpDownCell = CType(MyBase.Clone, NumericUpDownCell)
retVal.Minimum = Me.Minimum
retVal.Maximum = Me.Maximum
Return retVal
End Function
End Class
Second, inside the NumericUpDownCell classes InitializeEditingControl method, add the two lines:
ctl.Minimum = Me.Minimum
ctl.Maximum = Me.Maximum
When you setup the new column, get the CellTemplate to set the new properties, as per:
Dim upDownColumn As New NumericUpDownColumn
Dim cellTemplate As NumericUpDownCell = CType(upDownColumn .CellTemplate, NumericUpDownCell)
cellTemplate.Minimum = 10
cellTemplate.Maximum = 15
Or, following you preference to setup via the constructor, add a new constructor to the NumericUpdDownColumn class as per
Public Sub New(minValue As Integer, maxValue As Integer)
MyBase.New(New NumericUpDownCell())
Dim template As NumericUpDownCell = CType(CellTemplate, NumericUpDownCell)
template.Minimum = minValue
template.Maximum = maxValue
End Sub
and then use it like:
Dim upDownColumn As New NumericUpDownColumn(100, 150)
Dim cellTemplate As NumericUpDownCell = CType(upDownColumn.CellTemplate, NumericUpDownCell)
DataGridView1.Columns.Add(upDownColumn)

VB .Net - ShouldSerialize function

I have a problem with ShouldSerialize function.
I defined an usercontrol with a label (named Label1) on it.
Now i put the code below in the usercontrol class :
Imports System.ComponentModel
Public Class UserControl1
Dim _Range As UShort = 100
Private Sub UserControl1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = _Range
End Sub
Public Function ShouldSerializeTBValueRange() As Boolean
Return _Range <> 200
End Function
Public Sub ResetTBValueRange()
_Range = 200
Label1.Text = _Range
End Sub
Public Property TBValueRange As UShort
Get
Return _Range
End Get
Set(ByVal Steps As UShort)
_Range = Steps
Label1.Text = _Range
End Set
End Property
End Class
Now in a new form include the usercontrol.
In the properties grid of the usercontrol u can find the property TBValueRange.
If you right click in the property name you can Reinit the property.
After reinit u can see value 200 in the property.
Now, regenerate the project will reset the property to the initial value (id 100).
Why the value 200 didn't stay?
If i replace the line Return _Range <> 200 in the function ShouldSerializeTBValueRange() by
Return Not _Range.Equals(200)
it will work.
i don't understand.
Anyone could explain this?
The methods are working as coded. The thing which seems to not work as expected is the Not _Range.Equals(200) portion.
You initialize _Range to 100: Dim _Range As UShort = 100
Then, your ShouldSerializeTBValueRange method tells VS to only save the range value when it is not 200: Return _Range <> 200
So, when you reset it to 200 in the IDE, it wont save a value of 200, and the initial value of 100 displays.
The code uses one value for the default, 100 but use a different value for ShouldSerialize test. You should either change it to use a default of 200:
Dim _Range As UShort = 200
Or change the ShouldSerialize test to use 100:
Public Function ShouldSerializeTBValueRange() As Boolean
Return _Range <> 100
End Function
Treating the default value as the default, and using only one value for the default everything works as expected using just ShouldSerializexxx and Resetxxx with no need for anything else.
' THIS is the default value -
' ie the value that need not be saved because the
' control automatically starts with that value
Dim _Range As UShort = 200US
' Controls:
' - displays the prop value in Bold in the property window
' when the value is NOT the default
' - saves the value when it is NOT the default
' - enables/disables the RESET function
Public Function ShouldSerializeTBValueRange() As Boolean
Return (_Range <> 200US)
End Function
Using one value for the actual default (100) and then returning T/F based on a different value (200) results in
100 being saved when it need not be
200 not being saved when it should be
the Reset menu item and Bold value being in the incorrect state
The oddity is that Not _Range.Equals(200) seems to fail. It returns False after a reset (to 200) which causes the value to be saved when it really should not. There are 2 overloads for this:
UInt16.Equals(obj As Object) As Boolean
UInt16.Equals(v As UShort) As Boolean
If you pass anything other than an actual UInt16/UShort, the value will be boxed as Object. So, Not _Range.Equals(200) is using the first because 200 is Int32. The ILCode for that version is:
public override bool Equals(object obj)
{
return obj is ushort && this == (ushort)obj;
}
The test will fail the first test because obj contains an Int32. If you pass a UShort, it will work:
UShort.Equals(_Range, 200US)
'or:
_Range.Equals(200US)
All good reasons to understand the different data types, avoid boxing (As Object) and always and forever set Option Strict On.
Thanks you all to have spent time on my question.
I found my answer : when ShouldSerialize function return false the property value isn't save in the file Form1.designer.vb, so it gets the original value.
For my needed the ShouldSerialize function should always return true.
Public Function ShouldSerializeTBValueRange() As Boolean
Return true
End Function
So the property value will always be saved.
Now, what i'm looking for is how to control the context menu of the property grid.
I want the option "Reinit" greyed when the value is good.
Finally, i found something, with the use of PropertyDescriptor.
For whose are interested, we can save properties in the designer.vb file and test the value to reset.
here is my program, based on the article from MSDN - Microsft:
Imports System.ComponentModel
Imports System.Windows.Forms.Design
<Designer(GetType(DemoControlDesigner))>
Public Class UserControl1
Dim _MyProperty1 As String = "Test1"
Dim _MyProperty2 As Integer = 100
Dim _MyProperty3 As UShort = 200
Public Sub New()
InitializeComponent()
End Sub
Public Function CanResetMyProperty1() As Boolean
Return _MyProperty1 <> "Test"
End Function
Public Sub ResetMyProperty1()
_MyProperty1 = "Test"
End Sub
Public Property MyProperty1 As String
Get
Return _MyProperty1
End Get
Set(ByVal Value As String)
_MyProperty1 = Value
End Set
End Property
Public Function CanResetMyProperty2() As Boolean
Return _MyProperty2 <> 150
End Function
Public Sub ResetMyProperty2()
_MyProperty2 = 150
End Sub
Public Property MyProperty2 As Integer
Get
Return _MyProperty2
End Get
Set(ByVal Value As Integer)
_MyProperty2 = Value
End Set
End Property
Public Function CanResetMyProperty3() As Boolean
Return _MyProperty3 <> _MyProperty2
End Function
Public Sub ResetMyProperty3()
_MyProperty3 = _MyProperty2
End Sub
Public Property MyProperty3 As UShort
Get
Return _MyProperty3
End Get
Set(ByVal Value As UShort)
_MyProperty3 = Value
End Set
End Property
End Class
Friend NotInheritable Class SerializePropertyDescriptor
Inherits PropertyDescriptor
Private _pd As PropertyDescriptor = Nothing
Public Sub New(ByVal pd As PropertyDescriptor)
MyBase.New(pd)
_pd = pd
End Sub
Public Overrides ReadOnly Property Attributes() As AttributeCollection
Get
Return Me._pd.Attributes
End Get
End Property
Protected Overrides Sub FillAttributes(ByVal attributeList As IList)
MyBase.FillAttributes(attributeList)
End Sub
Public Overrides ReadOnly Property ComponentType() As Type
Get
Return Me._pd.ComponentType
End Get
End Property
Public Overrides ReadOnly Property Converter() As TypeConverter
Get
Return Me._pd.Converter
End Get
End Property
Public Overrides Function GetEditor(ByVal editorBaseType As Type) As Object
Return Me._pd.GetEditor(editorBaseType)
End Function
Public Overrides ReadOnly Property IsReadOnly() As Boolean
Get
Return Me._pd.IsReadOnly
End Get
End Property
Public Overrides ReadOnly Property PropertyType() As Type
Get
Return Me._pd.PropertyType
End Get
End Property
Public Overrides Function CanResetValue(ByVal component As Object) As Boolean
Try
Return CallByName(component, "CanReset" & _pd.Name, CallType.Get, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return False
End Function
Public Overrides Function GetValue(ByVal component As Object) As Object
Return Me._pd.GetValue(component)
End Function
Public Overrides Sub ResetValue(ByVal component As Object)
Me._pd.ResetValue(component)
End Sub
Public Overrides Sub SetValue(ByVal component As Object, ByVal val As Object)
Me._pd.SetValue(component, val)
End Sub
Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
Return True
End Function
End Class
Class DemoControlDesigner
Inherits ControlDesigner
Dim PropertiesToSerialize As String() = {"MyProperty1", "MyProperty2", "MyProperty3"}
Protected Overrides Sub PostFilterProperties(ByVal properties As IDictionary)
Dim original As PropertyDescriptor
For Each PropName As String In PropertiesToSerialize
If properties.Contains(PropName) Then
original = properties(PropName)
properties(PropName) = New SerializePropertyDescriptor(original)
End If
Next
MyBase.PostFilterProperties(properties)
End Sub
End Class
For each property that should be serialized and tested for reset, we should write a sub "CanResetPropertyName" which test the value to reset (see sample).
And the values stay now, even if we regenerate the project.
It's working fine for me, maybe it can be improved.
Regards.

VB.NET Databound Label not updating

I am having issues with a label with a databound text value updating. I have a Class that calculates the best throw of an athlete and this is bound to a label in my form. The class is as follows
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Public Class Competition
Public Sub New()
Competitors = New List(Of Competitor)()
End Sub
Public Property Competitors() As List(Of Competitor)
Get
Return m_Competitors
End Get
Set(value As List(Of Competitor))
m_Competitors = value
End Set
End Property
Private m_Competitors As List(Of Competitor)
Public ReadOnly Property CurrentPlacings() As List(Of Competitor)
Get
Return Competitors.OrderByDescending(Function(c) c.BestThrow).ToList()
End Get
End Property
End Class
Public Class Competitor
Implements System.ComponentModel.INotifyPropertyChanged
Public Event PropertyChanged(sender As Object,
e As System.ComponentModel.PropertyChangedEventArgs) _
Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Sub New()
Throws = New List(Of [Throw])()
End Sub
Public Property FirstName() As String
Get
Return m_FirstName
End Get
Set(value As String)
m_FirstName = value
End Set
End Property
Private m_FirstName As String
Public Property LastName() As String
Get
Return m_LastName
End Get
Set(value As String)
m_LastName = value
End Set
End Property
Private m_LastName As String
Public Property compNumber() As String
Get
Return m_compNumb
End Get
Set(value As String)
m_compNumb = value
End Set
End Property
Private m_compNumb As String
Public Property club() As String
Get
Return m_club
End Get
Set(value As String)
m_club = value
End Set
End Property
Private m_club As String
Public Property Throws() As List(Of [Throw])
Get
Return m_Throws
End Get
Set(value As List(Of [Throw]))
m_Throws = value
End Set
End Property
Private m_Throws As List(Of [Throw])
Public ReadOnly Property BestThrow() As Object
Get
Dim bt = Throws.Where(Function(t) t.Status = ThrowStatus.Valid).OrderByDescending(Function(t) t.Distance).First()
If (IsNothing(bt.Distance)) Then
bt.Distance = "0"
End If
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("BestThrow"))
Return bt
End Get
End Property
Public ReadOnly Property getLabel
Get
Return compNumber & " " & LastName & ", " & FirstName & vbCrLf & club
End Get
End Property
End Class
Public Enum ThrowStatus
Valid
Pass
Foul
End Enum
Public Class [Throw]
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property Status() As ThrowStatus
Get
Return m_Status
End Get
Set(value As ThrowStatus)
m_Status = value
End Set
End Property
Private m_Status As ThrowStatus
Public Property Distance() As String
Get
If Status = ThrowStatus.Valid Then
If (m_Distance > 0) Then
Return m_Distance
Else
Return Nothing
End If
ElseIf Status = ThrowStatus.Pass Then
Return "PASS"
ElseIf Status = ThrowStatus.Foul Then
Return "FOUL"
Else
Return Nothing
End If
End Get
Set(value As String)
If (value > 0) Then
If (IsNumeric(value)) Then
m_Distance = value
Status = ThrowStatus.Valid
ElseIf (value = "foul") Then
Status = ThrowStatus.Foul
ElseIf (value = "pass") Then
Status = ThrowStatus.Pass
Else
Status = ThrowStatus.Valid
m_Distance = Nothing
End If
Else
m_Distance = Nothing
Status = ThrowStatus.Valid
End If
OnPropertyChanged("Distance")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Private m_Distance As Decimal
Public Property attempt() As Integer
Get
Return m_attempt
End Get
Set(value As Integer)
m_attempt = value
End Set
End Property
Private m_attempt As Integer
End Class
The line that databinds for the Label is as follows:
best.DataBindings.Add(New Binding("text", athlete.BestThrow, "distance", False, DataSourceUpdateMode.OnPropertyChanged))
I can tell that the Property BestThrow is defintely being updated using Watch however for some reason the label seems to only reflect Throw(0).Distance and not BestThrow.Distance
I can change Throw(0) and the label will change, if I add a bigger number to any of the other 5 attempts and Watch the value I can see that the BestThrow is being updated.
Thanks in advance for your assistance.
Mark
I think you have a wrong prespective, property BestThrow is never updated because it has no setter. When you check BestThrow value using watch, it 'recalculated' because you request the value. What you need is to find a way to raise Property Changed event for BestThrow whenever item added or removed from Throws because BestThrow value depends on Throws. After event raised, UI will call BestThrow's getter again looking for updated value.
'You need to execute this code to notify UI that `BestThrow` value has changed
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("BestThrow"))
Raisng the event can be done manually after code for adding/removing item to/from Throws property, or you can make Throws an ObservableCollection of Throw then listen to Collection Changed event. Then in the event handler you can raise BestThrow property changed event.

Adding a margin to custom datagridview column

I've used the following code, taken from here, to create my own custom column for datagridview, so that I can now have images and text in the same cell:
Public Class TextAndImageColumn
Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private m_imageSize As Size
Public Sub New()
Me.CellTemplate = New TextAndImageCell
End Sub
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageColumn = TryCast(MyBase.Clone, TextAndImageColumn)
c.imageValue = Me.imageValue
c.m_imageSize = Me.m_imageSize
Return c
End Function
Public Property Image() As Image
Get
Return Me.imageValue
End Get
Set(ByVal value As Image)
Me.imageValue = value
Me.m_imageSize = value.Size
Dim inheritedPadding As Padding = Me.DefaultCellStyle.Padding
Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End Set
End Property
Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
Get
Return TryCast(Me.CellTemplate, TextAndImageCell)
End Get
End Property
Friend ReadOnly Property ImageSize() As Size
Get
Return m_imageSize
End Get
End Property
End Class
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size
Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageCell = TryCast(MyBase.Clone, TextAndImageCell)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function
Public Property Image() As Image
Get
If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
Else
If Not (Me.imageValue Is Nothing) Then
Return Me.imageValue
Else
Return Me.OwningTextAndImageColumn.Image
End If
End If
End Get
Set(ByVal value As Image)
Me.imageValue = value
Me.imageSize = value.Size
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End Set
End Property
Protected Overloads Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle, cellBounds As Rectangle,
rowIndex As Integer, cellState As DataGridViewElementStates, value As Object, formattedValue As Object,
errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle,
paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Not (Me.Image Is Nothing) Then
Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer
graphics.SetClip(cellBounds)
graphics.DrawImageUnscaled(Me.Image, cellBounds.Location)
graphics.EndContainer(container)
End If
End Sub
Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
Get
Return TryCast(Me.OwningColumn, TextAndImageColumn)
End Get
End Property
End Class
It works very well, except that the image that I use is right at the edges of the cell. I'd like to give it a small margin. How can I do this?
You could add a property to TextAndImageCell like :
Private m_imagePadding As New Padding(3)
Public Property ImagePadding() As Padding
Get
Return m_imagePadding
End Get
Set(ByVal value As Padding)
m_imagePadding = value
End Set
End Property
and implement (in Paint) like :
graphics.DrawImageUnscaled(Me.Image, _
New Point(cellBounds.Location.X + m_imagePadding.Left, _
cellBounds.Location.Y + m_imagePadding.Top))
also would need to change in TextAndImageColumn :
Me.DefaultCellStyle.Padding = New Padding(ImageSize.Width + _
TextAndImageCellTemplate.ImagePadding.Right, inheritedPadding.Top, _
inheritedPadding.Right, inheritedPadding.Bottom)
There's room for refinement, obviously (trigger redraws on padding change, sort out row heights, text padding, etc) but something like this should work.

Reflection: How to find from a property info object if that property has a Non Public (Private / Protected) Setter?

I searched on the forum / Internet for the solution how a PropetryInfo object (of a Public property) can reveal if it has a Private \ Protected Setter ... it was all in vain .... all help I found was about how to "Set" value of a public property having a Private Setter...
I would like to know if I have a PropertyInfo object of a public property, how would I know if its Setter is Non Public?
I tried, in a exception handling block, where I did a GetValue of the PropertyInfo object and then called SetValue by setting the same value back... but to my surprise it worked well and didn error out.
Help would be very much approaciated...
E.g.
Public Class Class1
Public Property HasContextChanged() As Boolean
Get
Return _hasContextChanged
End Get
Protected Set(ByVal value As Boolean)
_hasContextChanged = value
End Set
End Property
Public Function CanWriteProperty(Optional ByVal propName As String = "HasContextChanged") As Boolean
Dim prInfo As PropertyInfo = Me.GetType.GetProperty(propName)
If prInfo Is Nothing Then Return False
If Not prInfo.CanWrite Then
Return False
Else
Try
Dim value As Object = prInfo.GetValue(ownObj, Nothing)
prInfo.SetValue(ownObj, value, Nothing) 'Works for Private Setter
Catch ex As Exception
Return False 'Not coming here whatsoever
End Try
End If
Return True
End Function
End Class
Thx
Vinit sankhe.
Once you have the PropertyInfo for the property, you can call its GetSetMethod function to return the MethodInfo for the set accessor. You can then check the MethodInfo's IsPublic property to see if the set accessor is public.
Dim prInfo As PropertyInfo = Me.GetType.GetProperty(propName)
Dim method as MethodInfo = prInfo.GetSetMethod()
If Not method.IsPublic Then
Return False
Else
Dim value As Object = prInfo.GetValue(ownObj, Nothing)
prInfo.SetValue(ownObj, value, Nothing) 'Works for Private Setter
End If
In fact, then you both take a position to set and get method, here is an example:
Public Class ExPropertyInfo
Inherits PropertyInfo
Dim p As PropertyInfo
Public Sub New(ByVal [property] As PropertyInfo)
p = [property]
Dim accssors() As MethodInfo = [property].GetAccessors(True)
Select Case accssors.Length
Case 1
isassembly_ = accssors(0).IsAssembly
isfamily_ = accssors(0).IsFamily
isprivate_ = accssors(0).IsPrivate
ispublic_ = accssors(0).IsPublic
isstatic_ = accssors(0).IsStatic
isfamilyandassembly_ = accssors(0).IsFamilyAndAssembly
isfamilyorassembly_ = accssors(0).IsFamilyOrAssembly
Case 2
Dim method As MethodInfo = Nothing
If accssors(0).IsPrivate Then
If accssors(1).IsFamily Or accssors(1).IsFamilyAndAssembly Or accssors(1).IsFamilyOrAssembly Or accssors(1).IsAssembly Or accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
ElseIf accssors(0).IsFamily Then
If accssors(1).IsFamilyAndAssembly Or accssors(1).IsFamilyOrAssembly Or accssors(1).IsAssembly Or accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
ElseIf accssors(0).IsFamilyAndAssembly Or accssors(0).IsFamilyOrAssembly Then
If accssors(1).IsAssembly Or accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
ElseIf accssors(0).IsAssembly Or accssors(0).IsPublic Then
If accssors(1).IsPublic Then
method = accssors(1)
Else
method = accssors(0)
End If
End If
isassembly_ = method.IsAssembly
isfamily_ = method.IsFamily
isprivate_ = method.IsPrivate
ispublic_ = method.IsPublic
isstatic_ = method.IsStatic
isfamilyandassembly_ = method.IsFamilyAndAssembly
isfamilyorassembly_ = method.IsFamilyOrAssembly
End Select
End Sub
Public Overrides ReadOnly Property Attributes As Reflection.PropertyAttributes
Get
Return p.Attributes
End Get
End Property
Public Overrides ReadOnly Property CanRead As Boolean
Get
Return p.CanRead
End Get
End Property
Public Overrides ReadOnly Property CanWrite As Boolean
Get
Return p.CanWrite
End Get
End Property
Public Overrides ReadOnly Property DeclaringType As Type
Get
Return p.DeclaringType
End Get
End Property
Public Overloads Overrides Function GetAccessors(nonPublic As Boolean) As MethodInfo()
Return p.GetAccessors(nonPublic)
End Function
Public Overloads Overrides Function GetCustomAttributes(inherit As Boolean) As Object()
Return p.GetCustomAttributes(inherit)
End Function
Public Overloads Overrides Function GetCustomAttributes(attributeType As Type, inherit As Boolean) As Object()
Return p.GetCustomAttributes(attributeType, inherit)
End Function
Public Overloads Overrides Function GetGetMethod(nonPublic As Boolean) As MethodInfo
Return p.GetGetMethod(nonPublic)
End Function
Public Overrides Function GetIndexParameters() As ParameterInfo()
Return p.GetIndexParameters
End Function
Public Overloads Overrides Function GetSetMethod(nonPublic As Boolean) As MethodInfo
Return p.GetSetMethod(nonPublic)
End Function
Public Overloads Overrides Function GetValue(obj As Object, invokeAttr As BindingFlags, binder As Binder, index() As Object, culture As Globalization.CultureInfo) As Object
Return p.GetValue(obj, invokeAttr, binder, index, culture)
End Function
Public Overrides Function IsDefined(attributeType As Type, inherit As Boolean) As Boolean
Return p.IsDefined(attributeType, inherit)
End Function
Public Overrides ReadOnly Property Name As String
Get
Return p.Name
End Get
End Property
Public Overrides ReadOnly Property PropertyType As Type
Get
Return p.PropertyType
End Get
End Property
Public Overrides ReadOnly Property ReflectedType As Type
Get
Return p.ReflectedType
End Get
End Property
Public Overloads Overrides Sub SetValue(obj As Object, value As Object, invokeAttr As BindingFlags, binder As Binder, index() As Object, culture As Globalization.CultureInfo)
p.SetValue(obj, value, invokeAttr, binder, index, culture)
End Sub
Private ispublic_ As Boolean
Public ReadOnly Property IsPublic As Boolean
Get
Return ispublic_
End Get
End Property
Private isprivate_ As Boolean
Public ReadOnly Property IsPrivate As Boolean
Get
Return isprivate_
End Get
End Property
Private isassembly_ As Boolean
Public ReadOnly Property IsAssembly As Boolean
Get
Return isassembly_
End Get
End Property
Private isfamily_ As Boolean
Public ReadOnly Property IsFamily As Boolean
Get
Return isfamily_
End Get
End Property
Private isstatic_ As Boolean
Public ReadOnly Property IsStatic As Boolean
Get
Return isstatic_
End Get
End Property
Private isfamilyandassembly_ As Boolean
Public ReadOnly Property IsFamilyAndAssembly As Boolean
Get
Return isfamilyandassembly_
End Get
End Property
Private isfamilyorassembly_ As Boolean
Public ReadOnly Property IsFamilyOrAssembly As Boolean
Get
Return isfamilyorassembly_
End Get
End Property
End Class
Dim [property] As ExPropertyInfo = New ExPropertyInfo(GetType(Form1).GetProperty("abc", BindingFlags.NonPublic Or BindingFlags.Static))
PropertyInfo.GetAccessors the method returns a array of get and set the method.